<!DOCTYPE HTML>
<html lang="zh-CN">


<head>
    <meta charset="utf-8">
    <meta name="keywords" content="Java基础篇（二）, Angus">
    <meta name="description" content="Java基础入门篇">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
    <meta name="renderer" content="webkit|ie-stand|ie-comp">
    <meta name="mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
    <meta name="referrer" content="no-referrer-when-downgrade">
    <!-- Global site tag (gtag.js) - Google Analytics -->


    

    <title>Java基础篇（二） | Angus</title>
    <link rel="icon" type="image/png" href="/favicon.png">

    <link rel="stylesheet" type="text/css" href="/libs/awesome/css/all.min.css">
    <link rel="stylesheet" type="text/css" href="/libs/materialize/materialize.min.css">
    <link rel="stylesheet" type="text/css" href="/libs/aos/aos.css">
    <link rel="stylesheet" type="text/css" href="/libs/animate/animate.min.css">
    <link rel="stylesheet" type="text/css" href="/libs/lightGallery/css/lightgallery.min.css">
    <link rel="stylesheet" type="text/css" href="/css/matery.css">
    <link rel="stylesheet" type="text/css" href="/css/my.css">

    <script src="/libs/jquery/jquery-3.6.0.min.js"></script>

<meta name="generator" content="Hexo 6.1.0">
<style>.github-emoji { position: relative; display: inline-block; width: 1.2em; min-height: 1.2em; overflow: hidden; vertical-align: top; color: transparent; }  .github-emoji > span { position: relative; z-index: 10; }  .github-emoji img, .github-emoji .fancybox { margin: 0 !important; padding: 0 !important; border: none !important; outline: none !important; text-decoration: none !important; user-select: none !important; cursor: auto !important; }  .github-emoji img { height: 1.2em !important; width: 1.2em !important; position: absolute !important; left: 50% !important; top: 50% !important; transform: translate(-50%, -50%) !important; user-select: none !important; cursor: auto !important; } .github-emoji-fallback { color: inherit; } .github-emoji-fallback img { opacity: 0 !important; }</style>
<link rel="alternate" href="/atom.xml" title="Angus" type="application/atom+xml">
<link rel="stylesheet" href="/css/prism-tomorrow.css" type="text/css"></head>


<body>
    <header class="navbar-fixed">
    <nav id="headNav" class="bg-color nav-transparent">
        <div id="navContainer" class="nav-wrapper container">
            <div class="brand-logo">
                <a href="/" class="waves-effect waves-light">
                    
                    <img src="/medias/logo.png" class="logo-img" alt="LOGO">
                    
                    <span class="logo-span">Angus</span>
                </a>
            </div>
            

<a href="#" data-target="mobile-nav" class="sidenav-trigger button-collapse"><i class="fas fa-bars"></i></a>
<ul class="right nav-menu">
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/" class="waves-effect waves-light">
      
      <i class="fas fa-home" style="zoom: 0.6;"></i>
      
      <span>首页</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/tags" class="waves-effect waves-light">
      
      <i class="fas fa-tags" style="zoom: 0.6;"></i>
      
      <span>标签</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/categories" class="waves-effect waves-light">
      
      <i class="fas fa-bookmark" style="zoom: 0.6;"></i>
      
      <span>分类</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/archives" class="waves-effect waves-light">
      
      <i class="fas fa-archive" style="zoom: 0.6;"></i>
      
      <span>归档</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/about" class="waves-effect waves-light">
      
      <i class="fas fa-user-circle" style="zoom: 0.6;"></i>
      
      <span>关于</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/contact" class="waves-effect waves-light">
      
      <i class="fas fa-comments" style="zoom: 0.6;"></i>
      
      <span>留言板</span>
    </a>
    
  </li>
  
  <li>
    <a href="#searchModal" class="modal-trigger waves-effect waves-light">
      <i id="searchIcon" class="fas fa-search" title="搜索" style="zoom: 0.85;"></i>
    </a>
  </li>
</ul>


<div id="mobile-nav" class="side-nav sidenav">

    <div class="mobile-head bg-color">
        
        <img src="/medias/logo.png" class="logo-img circle responsive-img">
        
        <div class="logo-name">Angus</div>
        <div class="logo-desc">
            
            never give up!
            
        </div>
    </div>

    <ul class="menu-list mobile-menu-list">
        
        <li class="m-nav-item">
	  
		<a href="/" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-home"></i>
			
			首页
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/tags" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-tags"></i>
			
			标签
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/categories" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-bookmark"></i>
			
			分类
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/archives" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-archive"></i>
			
			归档
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/about" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-user-circle"></i>
			
			关于
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/contact" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-comments"></i>
			
			留言板
		</a>
          
        </li>
        
        
        <li><div class="divider"></div></li>
        <li>
            <a href="https://github.com/Monkey-chao/Monkey-chao.github.io.git" class="waves-effect waves-light" target="_blank">
                <i class="fab fa-github-square fa-fw"></i>Fork Me
            </a>
        </li>
        
    </ul>
</div>


        </div>

        
            <style>
    .nav-transparent .github-corner {
        display: none !important;
    }

    .github-corner {
        position: absolute;
        z-index: 10;
        top: 0;
        right: 0;
        border: 0;
        transform: scale(1.1);
    }

    .github-corner svg {
        color: #0f9d58;
        fill: #fff;
        height: 64px;
        width: 64px;
    }

    .github-corner:hover .octo-arm {
        animation: a 0.56s ease-in-out;
    }

    .github-corner .octo-arm {
        animation: none;
    }

    @keyframes a {
        0%,
        to {
            transform: rotate(0);
        }
        20%,
        60% {
            transform: rotate(-25deg);
        }
        40%,
        80% {
            transform: rotate(10deg);
        }
    }
</style>

<a href="https://github.com/Monkey-chao/Monkey-chao.github.io.git" class="github-corner tooltipped hide-on-med-and-down" target="_blank"
   data-tooltip="Fork Me" data-position="left" data-delay="50">
    <svg viewBox="0 0 250 250" aria-hidden="true">
        <path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path>
        <path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2"
              fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path>
        <path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z"
              fill="currentColor" class="octo-body"></path>
    </svg>
</a>
        
    </nav>

</header>

    



<div class="bg-cover pd-header post-cover" style="background-image: url('/medias/featureimages/2.jpg')">
    <div class="container" style="right: 0px;left: 0px;">
        <div class="row">
            <div class="col s12 m12 l12">
                <div class="brand">
                    <h1 class="description center-align post-title">Java基础篇（二）</h1>
                </div>
            </div>
        </div>
    </div>
</div>




<main class="post-container content">

    
    <link rel="stylesheet" href="/libs/tocbot/tocbot.css">
<style>
    #articleContent h1::before,
    #articleContent h2::before,
    #articleContent h3::before,
    #articleContent h4::before,
    #articleContent h5::before,
    #articleContent h6::before {
        display: block;
        content: " ";
        height: 100px;
        margin-top: -100px;
        visibility: hidden;
    }

    #articleContent :focus {
        outline: none;
    }

    .toc-fixed {
        position: fixed;
        top: 64px;
    }

    .toc-widget {
        width: 345px;
        padding-left: 20px;
    }

    .toc-widget .toc-title {
        padding: 35px 0 15px 17px;
        font-size: 1.5rem;
        font-weight: bold;
        line-height: 1.5rem;
    }

    .toc-widget ol {
        padding: 0;
        list-style: none;
    }

    #toc-content {
        padding-bottom: 30px;
        overflow: auto;
    }

    #toc-content ol {
        padding-left: 10px;
    }

    #toc-content ol li {
        padding-left: 10px;
    }

    #toc-content .toc-link:hover {
        color: #42b983;
        font-weight: 700;
        text-decoration: underline;
    }

    #toc-content .toc-link::before {
        background-color: transparent;
        max-height: 25px;

        position: absolute;
        right: 23.5vw;
        display: block;
    }

    #toc-content .is-active-link {
        color: #42b983;
    }

    #floating-toc-btn {
        position: fixed;
        right: 15px;
        bottom: 76px;
        padding-top: 15px;
        margin-bottom: 0;
        z-index: 998;
    }

    #floating-toc-btn .btn-floating {
        width: 48px;
        height: 48px;
    }

    #floating-toc-btn .btn-floating i {
        line-height: 48px;
        font-size: 1.4rem;
    }
</style>
<div class="row">
    <div id="main-content" class="col s12 m12 l9">
        <!-- 文章内容详情 -->
<div id="artDetail">
    <div class="card">
        <div class="card-content article-info">
            <div class="row tag-cate">
                <div class="col s7">
                    
                    <div class="article-tag">
                        
                            <a href="/tags/Java/">
                                <span class="chip bg-color">Java</span>
                            </a>
                        
                            <a href="/tags/%E6%95%99%E7%A8%8B/">
                                <span class="chip bg-color">教程</span>
                            </a>
                        
                            <a href="/tags/%E5%9F%BA%E7%A1%80/">
                                <span class="chip bg-color">基础</span>
                            </a>
                        
                    </div>
                    
                </div>
                <div class="col s5 right-align">
                    
                    <div class="post-cate">
                        <i class="fas fa-bookmark fa-fw icon-category"></i>
                        
                            <a href="/categories/Java%E5%9F%BA%E7%A1%80%E6%95%99%E7%A8%8B/" class="post-category">
                                Java基础教程
                            </a>
                        
                    </div>
                    
                </div>
            </div>

            <div class="post-info">
                
                <div class="post-date info-break-policy">
                    <i class="far fa-calendar-minus fa-fw"></i>发布日期:&nbsp;&nbsp;
                    2020-05-07
                </div>
                

                
                <div class="post-date info-break-policy">
                    <i class="far fa-calendar-check fa-fw"></i>更新日期:&nbsp;&nbsp;
                    2022-06-16
                </div>
                

                
                <div class="info-break-policy">
                    <i class="far fa-file-word fa-fw"></i>文章字数:&nbsp;&nbsp;
                    11.4k
                </div>
                

                
                <div class="info-break-policy">
                    <i class="far fa-clock fa-fw"></i>阅读时长:&nbsp;&nbsp;
                    46 分
                </div>
                

                
                    <div id="busuanzi_container_page_pv" class="info-break-policy">
                        <i class="far fa-eye fa-fw"></i>阅读次数:&nbsp;&nbsp;
                        <span id="busuanzi_value_page_pv"></span>
                    </div>
				
            </div>
        </div>
        <hr class="clearfix">

        

        

        <div class="card-content article-card-content">
            <div id="articleContent">
                <h1 id="Java基础篇（二）"><a href="#Java基础篇（二）" class="headerlink" title="Java基础篇（二）"></a>Java基础篇（二）</h1><h1 id="一、数组"><a href="#一、数组" class="headerlink" title="一、数组"></a>一、数组</h1><h2 id="1-API"><a href="#1-API" class="headerlink" title="1.API"></a>1.API</h2><p>API（Application Programming Interface应用程序接口）是一些预先定义的接口。</p>
<h2 id="2-数组概述"><a href="#2-数组概述" class="headerlink" title="2.数组概述"></a>2.数组概述</h2><ul>
<li>数组是用来存储同一类型若干数据的容器</li>
<li>数组是引用类型</li>
<li>数组里面的数据叫元素</li>
<li>数组的长度是固定的，一旦声明就不可变</li>
<li>数组每个容器在内存中<strong>连续</strong>的存储（每个容器的地址是连续的），则使用<strong>下标</strong>定位元素</li>
<li>下标是整数，且从0开始</li>
</ul>
<h2 id="3-数组基础语法"><a href="#3-数组基础语法" class="headerlink" title="3.数组基础语法"></a>3.数组基础语法</h2><blockquote>
<p>声明<br>数据类型[] 数组名； </p>
<p>初始化<br>内存初始化<br>   数据类型[] 数组名=new 数据类型[长度]；<br> 元素初始化<br>   简写方式1：数据类型[] 数组名=new 数据类型[]{元素列表}<br>   简写方式2：数据类型[] 数组名={元素列表}（但是只能在声明的时候使用）</p>
<p>数组元素的默认值<br>数组内存初始化的时候，数组的容器内的元素是<strong>零值</strong></p>
<ul>
<li>值类型中的数字类型零值是0</li>
<li>值类型中的boolean零值是false</li>
<li>引用类型的零值是null</li>
</ul>
</blockquote>
<h2 id="4-数组的操作"><a href="#4-数组的操作" class="headerlink" title="4.数组的操作"></a>4.数组的操作</h2><blockquote>
<p>数组元素的修改<br>数组名[下标]=数据；</p>
<p>数组某一个元素的查询（读取）<br>数组名[下标]；</p>
<p>数组全部元素的查询（遍历）<br>循环下标</p>
</blockquote>
<h2 id="5-数组相关问题"><a href="#5-数组相关问题" class="headerlink" title="5.数组相关问题"></a>5.数组相关问题</h2><blockquote>
<p>数组异常<br>数组下标超过数组的容量（下标越界）</p>
<ul>
<li>java.lang.ArrayIndexOutOfBoundsException</li>
</ul>
<p>数组对象的属性<br>length：表示数组的长度</p>
<p>数组特点总结</p>
<ul>
<li>数组长度固定</li>
<li>数组有下标</li>
</ul>
<p>所有接口父类都是object，接口的extends没有继承概念，只是组合概念</p>
<p>数组的内存存储是连续的</p>
</blockquote>
<h2 id="6-基本项目开发思路"><a href="#6-基本项目开发思路" class="headerlink" title="6.基本项目开发思路"></a>6.基本项目开发思路</h2><p>基本程序开发思路</p>
<ul>
<li>创建入口包（app）<ul>
<li>创建一个main方法</li>
</ul>
</li>
<li>创建实体包（entity，beans）<ul>
<li>根据需求封装具体的业务操作类</li>
</ul>
</li>
<li>创建数据包（dao）<ul>
<li>负责数据</li>
</ul>
</li>
<li>创建业务包（services）<ul>
<li>负责业务逻辑<ul>
<li>创建业务逻辑接口</li>
<li>创建业务逻辑接口实现（实现的细节）</li>
</ul>
</li>
</ul>
</li>
</ul>
<p>基本程序开发各个包之间的依赖关系</p>
<p>业务包依赖数据包</p>
<p>入口包依赖业务包</p>
<h2 id="7-可变长参数"><a href="#7-可变长参数" class="headerlink" title="7.可变长参数"></a>7.可变长参数</h2><p>语法</p>
<pre class=" language-java"><code class="language-java"><span class="token comment" spellcheck="true">//三点形参</span>
<span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">fun3</span><span class="token punctuation">(</span><span class="token keyword">int</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span> a<span class="token punctuation">)</span><span class="token punctuation">{</span>
<span class="token punctuation">}</span>
</code></pre>
<p>三点参数为变成参数，只能在参数上出现三点</p>
<p>三点参数的实参可以是数组类型，也可以是非数组类型（使用多个实参逗号隔开）</p>
<pre class=" language-java"><code class="language-java"><span class="token comment" spellcheck="true">//可以是数组类型</span>
<span class="token keyword">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span> a3<span class="token operator">=</span><span class="token keyword">new</span> <span class="token class-name">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">{</span><span class="token number">10</span><span class="token punctuation">,</span><span class="token number">20</span><span class="token punctuation">,</span><span class="token number">30</span><span class="token punctuation">,</span><span class="token number">40</span><span class="token punctuation">}</span><span class="token punctuation">;</span>
myApp<span class="token punctuation">.</span>demoServices<span class="token punctuation">.</span><span class="token function">fun3</span><span class="token punctuation">(</span>a3<span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment" spellcheck="true">//单个非数组类型实参</span>
<span class="token keyword">int</span> a2<span class="token operator">=</span><span class="token number">2000</span><span class="token punctuation">;</span>
myApp<span class="token punctuation">.</span>demoServices<span class="token punctuation">.</span><span class="token function">fun3</span><span class="token punctuation">(</span>a2<span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment" spellcheck="true">//多个非数组类型实参</span>
<span class="token keyword">int</span> a2_1<span class="token operator">=</span><span class="token number">2000</span><span class="token punctuation">;</span>
<span class="token keyword">int</span> a2_2<span class="token operator">=</span><span class="token number">3000</span><span class="token punctuation">;</span>
<span class="token keyword">int</span> a2_3<span class="token operator">=</span><span class="token number">4000</span><span class="token punctuation">;</span>
myApp<span class="token punctuation">.</span>demoServices<span class="token punctuation">.</span><span class="token function">fun3</span><span class="token punctuation">(</span>a2_1<span class="token punctuation">,</span>a2_2<span class="token punctuation">,</span>a2_3<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>
<p>三点参数的形参与非三点参数一起定义的时候，只能放在后面</p>
<pre class=" language-java"><code class="language-java"><span class="token comment" spellcheck="true">//todo 三点参数与非三点参数一起</span>
<span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">fun4</span><span class="token punctuation">(</span><span class="token keyword">int</span> b<span class="token punctuation">,</span><span class="token keyword">int</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span> a<span class="token punctuation">)</span><span class="token punctuation">{</span>
<span class="token punctuation">}</span>
</code></pre>
<p>三点参数的形参个数只能有一个</p>
<h2 id="8-数组算法"><a href="#8-数组算法" class="headerlink" title="8.数组算法"></a>8.数组算法</h2><h3 id="1-冒泡排序"><a href="#1-冒泡排序" class="headerlink" title="[1]冒泡排序"></a>[1]冒泡排序</h3><p>两两交换</p>
<pre class=" language-java"><code class="language-java">冒泡排序是比较基础的排序算法之一，其思想是相邻的元素两两比较，较大的数下沉，较小的数冒起来，这样一趟比较下来，最大<span class="token punctuation">(</span>小<span class="token punctuation">)</span>值就会排列在一端。整个过程如同气泡冒起，因此被称作冒泡排序。
<span class="token comment" spellcheck="true">//todo 冒泡排序</span>
<span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">maopao</span><span class="token punctuation">(</span><span class="token keyword">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span> a<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token comment" spellcheck="true">//因为两两比较因此要使用槽长（也就是总长-1），</span>
    <span class="token keyword">int</span> z<span class="token operator">=</span>a<span class="token punctuation">.</span>length <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">;</span>
    <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span>z <span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> j <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> j <span class="token operator">&lt;</span> z<span class="token operator">-</span>i<span class="token punctuation">;</span> j<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token keyword">if</span> <span class="token punctuation">(</span>a<span class="token punctuation">[</span>j<span class="token punctuation">]</span> <span class="token operator">></span> a<span class="token punctuation">[</span>j <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                <span class="token keyword">int</span> temp <span class="token operator">=</span> a<span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">;</span>
                <span class="token comment" spellcheck="true">//体现两两交换</span>
                a<span class="token punctuation">[</span>j<span class="token punctuation">]</span> <span class="token operator">=</span> a<span class="token punctuation">[</span>j <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
                a<span class="token punctuation">[</span>j <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">]</span> <span class="token operator">=</span> temp<span class="token punctuation">;</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<h3 id="2-线性排序-x2F-选择排序"><a href="#2-线性排序-x2F-选择排序" class="headerlink" title="[2]线性排序/选择排序"></a>[2]线性排序/选择排序</h3><p>每个元素线性与其它元素依次比较</p>
<pre class=" language-java"><code class="language-java">选择排序是一种简单直观的排序算法。它的工作原理是：第一次从待排序的数据元素中选出最小（或最大）的一个元素，存放在序列的起始位置，然后再从剩余的未排序元素中寻找到最小（大）元素，继续放在起始位置，直到未排序元素个数为<span class="token number">0</span>。
<span class="token comment" spellcheck="true">//选择排序</span>
<span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">line</span><span class="token punctuation">(</span><span class="token keyword">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span> a<span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token keyword">int</span> z<span class="token operator">=</span>a<span class="token punctuation">.</span>length<span class="token punctuation">;</span>
    <span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span>i<span class="token operator">&lt;</span>z<span class="token punctuation">;</span>i<span class="token operator">++</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
        <span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> j<span class="token operator">=</span>i<span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">;</span>j<span class="token operator">&lt;</span>z<span class="token punctuation">;</span>j<span class="token operator">++</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
            <span class="token keyword">if</span><span class="token punctuation">(</span>a<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token operator">></span>a<span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
                <span class="token keyword">int</span> temp<span class="token operator">=</span>a<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">;</span>
                a<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token operator">=</span>a<span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">;</span>
                a<span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token operator">=</span>temp<span class="token punctuation">;</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<p>冒泡排序和选择排序程序上的区别</p>
<ul>
<li>冒泡两两比较，元素都在尾部冒出</li>
<li>选择线性比较，元素都在头部选出</li>
</ul>
<h3 id="3-二分查找"><a href="#3-二分查找" class="headerlink" title="[3]二分查找"></a>[3]二分查找</h3><ul>
<li>前置条件<ul>
<li>需要先排好序【升序】</li>
</ul>
</li>
<li>需要头指针（下标）、中指针（下标）、尾指针（下标）<ul>
<li>头指针（下标）初始值为0，也就是数组的第一个下标</li>
<li>尾指针（下标）初始值为数组长度-1</li>
<li>中指针（下标）初始值为（头指针+尾指针）/2 【注意这里是取整】</li>
</ul>
</li>
<li>查询规则<ul>
<li>待查询的数与中指针相等视为查到</li>
<li>大于<ul>
<li><strong>中指针前进（+1）</strong></li>
<li>头指针等于中指针</li>
<li>然后计算中指针【（头指针+尾指针）/2 】</li>
<li>继续查询</li>
</ul>
</li>
<li>小于<ul>
<li><strong>中指针后退（-1）</strong></li>
<li>尾指针等于中指针</li>
<li>然后计算中指针【（头指针+尾指针）/2 】</li>
<li>继续查询</li>
</ul>
</li>
<li>继续查询的条件<ul>
<li>头指针&lt;=尾头指针</li>
</ul>
</li>
</ul>
</li>
</ul>
<pre class=" language-java"><code class="language-java"><span class="token comment" spellcheck="true">//todo 二分查找</span>
<span class="token keyword">public</span> <span class="token keyword">int</span> <span class="token function">binarySearch</span><span class="token punctuation">(</span><span class="token keyword">int</span> number<span class="token punctuation">,</span><span class="token keyword">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span> a<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token comment" spellcheck="true">//二分查找数组先排序</span>
    <span class="token comment" spellcheck="true">//先冒泡排序</span>
    <span class="token function">maopao</span><span class="token punctuation">(</span>a<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment" spellcheck="true">//返回初始值</span>
    <span class="token keyword">int</span> result<span class="token operator">=</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">;</span>
    <span class="token comment" spellcheck="true">//开始初始化三个指针</span>
    <span class="token comment" spellcheck="true">//头指针</span>
    <span class="token keyword">int</span> from<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span>
    <span class="token comment" spellcheck="true">//尾指针</span>
    <span class="token keyword">int</span> end<span class="token operator">=</span>a<span class="token punctuation">.</span>length<span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">;</span>
    <span class="token comment" spellcheck="true">//中指针</span>
    <span class="token keyword">int</span> mid<span class="token operator">=</span><span class="token punctuation">(</span>from<span class="token operator">+</span>end<span class="token punctuation">)</span><span class="token operator">/</span><span class="token number">2</span><span class="token punctuation">;</span>
    <span class="token comment" spellcheck="true">//开始查询</span>
    <span class="token keyword">while</span><span class="token punctuation">(</span>from<span class="token operator">&lt;=</span>end<span class="token punctuation">)</span><span class="token punctuation">{</span>
        <span class="token comment" spellcheck="true">//数据和中指针判断</span>
        <span class="token keyword">if</span><span class="token punctuation">(</span>number<span class="token operator">==</span>a<span class="token punctuation">[</span>mid<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
            <span class="token comment" spellcheck="true">//相等情况</span>
            <span class="token comment" spellcheck="true">// 找到啦</span>
            result<span class="token operator">=</span>mid<span class="token punctuation">;</span>
            <span class="token comment" spellcheck="true">//这里需要注意找到数据后</span>
            <span class="token comment" spellcheck="true">//需要退出循环，不然死循环，因为中指针不前进和不后退了</span>
            <span class="token keyword">break</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
        <span class="token keyword">else</span> <span class="token keyword">if</span><span class="token punctuation">(</span>number<span class="token operator">></span>a<span class="token punctuation">[</span>mid<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
            <span class="token comment" spellcheck="true">//大于情况</span>
            <span class="token comment" spellcheck="true">//中指针前进</span>
            mid<span class="token operator">+=</span><span class="token number">1</span><span class="token punctuation">;</span>
            <span class="token comment" spellcheck="true">//头指针等于中指针位置</span>
            from<span class="token operator">=</span>mid<span class="token punctuation">;</span>
            <span class="token comment" spellcheck="true">//从新计算中指针</span>
            mid<span class="token operator">=</span><span class="token punctuation">(</span>from<span class="token operator">+</span>end<span class="token punctuation">)</span><span class="token operator">/</span><span class="token number">2</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
        <span class="token keyword">else</span> <span class="token punctuation">{</span>
            <span class="token comment" spellcheck="true">//小于情况</span>
            <span class="token comment" spellcheck="true">//中指针后退</span>
            mid<span class="token operator">-=</span><span class="token number">1</span><span class="token punctuation">;</span>
            <span class="token comment" spellcheck="true">//尾指针等于中指针位置</span>
            end<span class="token operator">=</span>mid<span class="token punctuation">;</span>
            <span class="token comment" spellcheck="true">//从新计算中指针</span>
            mid<span class="token operator">=</span><span class="token punctuation">(</span>from<span class="token operator">+</span>end<span class="token punctuation">)</span><span class="token operator">/</span><span class="token number">2</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
    <span class="token comment" spellcheck="true">//返回查询到的下标</span>
    <span class="token keyword">return</span> result<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
<h2 id="9-二维数组"><a href="#9-二维数组" class="headerlink" title="9.二维数组"></a>9.二维数组</h2><ul>
<li>一维数组里面的每个元素又是一个数组</li>
<li>二维数组的长度可以任意，换句话说java的二维数组是不规则数组</li>
<li>二维数组初始化<ul>
<li>必须先确定一维度数组长度</li>
</ul>
</li>
</ul>
<h2 id="10-数组工具类"><a href="#10-数组工具类" class="headerlink" title="10.数组工具类"></a>10.数组工具类</h2><p>Arrays数组工具类，提供一些静态的各种操作数组的方法</p>
<ul>
<li>冒泡排序【sort】</li>
<li>二分查找【binarySearch】</li>
<li>数组拷贝【copyOf】</li>
</ul>
<h2 id="11-数组扩容"><a href="#11-数组扩容" class="headerlink" title="11.数组扩容"></a>11.数组扩容</h2><h1 id="二、常用类"><a href="#二、常用类" class="headerlink" title="二、常用类"></a>二、常用类</h1><h2 id="1-String字符串类"><a href="#1-String字符串类" class="headerlink" title="1.String字符串类"></a>1.String字符串类</h2><h3 id="1-字符串常量池"><a href="#1-字符串常量池" class="headerlink" title="[1]字符串常量池"></a>[1]字符串常量池</h3><blockquote>
<p>内存中有一个区域为字符串常量池，主要是为了优化字符串的</p>
<ul>
<li>创建字符串的时候先搜索字符串常量池是否有字符串序列，如果没有则创建</li>
<li>字符串对象使用=运算符的时候实际上是指向字符串常量池</li>
<li>java提供一个intern()方法类返回一个字符串常量池的引用</li>
</ul>
</blockquote>
<h3 id="2-String创建方式"><a href="#2-String创建方式" class="headerlink" title="[2]String创建方式"></a>[2]String创建方式</h3><ul>
<li>字面量创建方法<ul>
<li>字面量创建方法中可以使用字符串常量池来达到优化的目的</li>
<li>创建一个新字符串对象时，会先查询字符串常量池是否拥有相等的字符串序列，如果有就不会创建新对象</li>
</ul>
</li>
</ul>
<pre class=" language-java"><code class="language-java"><span class="token comment" spellcheck="true">//字面量创建字符串</span>
String str1<span class="token operator">=</span><span class="token string">"hzj"</span><span class="token punctuation">;</span>

<span class="token operator">*</span> <span class="token keyword">new</span>关键字创建方法
  <span class="token operator">*</span> 任何时刻都会创建新对象

<span class="token comment" spellcheck="true">//使用new关键字创建字符串</span>
String str2<span class="token operator">=</span><span class="token keyword">new</span> <span class="token class-name">String</span><span class="token punctuation">(</span><span class="token string">"hzj"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>
<h3 id="3-intern-方法"><a href="#3-intern-方法" class="headerlink" title="[3]intern()方法"></a>[3]intern()方法</h3><p>该方法返回的字符串序列指向的是字符串常量池中的字符串</p>
<h3 id="4-equals-方法"><a href="#4-equals-方法" class="headerlink" title="[4]equals()方法"></a>[4]equals()方法</h3><p>比较字符串序列是否相等，不关系内存地址</p>
<h3 id="5-字符特殊符号-号"><a href="#5-字符特殊符号-号" class="headerlink" title="[5]字符特殊符号+号"></a>[5]字符特殊符号+号</h3><ul>
<li>字符串+号是连接字符串</li>
<li>字符串对象的字符串序列是不可变的</li>
<li>+号连接内部字节码指令规则<ul>
<li>链接的字符串都是字面量，则+号实际上省略【字符串常量池中】</li>
<li>链接的字符串有任意一个变量，则立即创建StringBuilder对象，然后使用append方法连接字符串，最后使用toString方法返回新字符串对象【堆中】</li>
</ul>
</li>
</ul>
<h3 id="6-字符串总结"><a href="#6-字符串总结" class="headerlink" title="[6]字符串总结"></a>[6]字符串总结</h3><ul>
<li>String类是final</li>
<li>String对象可以存在<strong>堆</strong>或<strong>字符串常量池</strong></li>
<li><strong>String对象是常量</strong></li>
<li><strong>String对象相加，全部字面量相加结果在字符串常量池，否则结果为新字符串在堆</strong></li>
</ul>
<ul>
<li>String类无法继承      public final class String</li>
</ul>
<h2 id="2-Date日期类"><a href="#2-Date日期类" class="headerlink" title="2.Date日期类"></a>2.Date日期类</h2><h3 id="1-Date"><a href="#1-Date" class="headerlink" title="[1]Date"></a>[1]Date</h3><ul>
<li>前日期距离1970-1-1 00:00:00 经过的毫秒数即为日期</li>
<li>存储日期的</li>
<li>提供一个getTime返回日期的毫秒</li>
</ul>
<h3 id="2-SimpleDateFormat"><a href="#2-SimpleDateFormat" class="headerlink" title="[2]SimpleDateFormat"></a>[2]SimpleDateFormat</h3><p>日期格式化的返回的是字符串</p>
<h3 id="3-Calendar"><a href="#3-Calendar" class="headerlink" title="[3]Calendar"></a>[3]Calendar</h3><ul>
<li>日历负责拆分日期，并提供计算功能<ul>
<li>Calendar提供了若干字段来表示日期的部分</li>
<li>Calendar提供了get方法获取日期的部分</li>
<li>Calendar提供set方法类设置一个新的日期</li>
<li>Calendar提供add方法增加或者减少日期</li>
</ul>
</li>
<li>Calendar提供getTime返回Date对象</li>
</ul>
<h3 id="新日期架构"><a href="#新日期架构" class="headerlink" title="新日期架构"></a>新日期架构</h3><p>机器时间概念–毫秒数</p>
<p>LocalDate日期</p>
<p>LocalTime时间</p>
<p>LocalDateTime日期和时间</p>
<h2 id="3-Math数学类"><a href="#3-Math数学类" class="headerlink" title="3.Math数学类"></a>3.Math数学类</h2><p>字段常量</p>
<ul>
<li><p>数学API</p>
<p>提供了数学的支持</p>
</li>
<li><p>math提供的全部都是静态方法无需实例化</p>
<p>math常量PI</p>
<p>pow方法,求数的几次方</p>
<p>floor取整，ceil进位，round四舍五入(double类型)</p>
</li>
<li><p>随机数(生成小数)</p>
<p>Math.random()    大于等于0到小于1.0</p>
<p>Math.random()*26   0到25</p>
</li>
<li><p>大数字支持</p>
</li>
</ul>
<p>BigDecimal类(小数)</p>
<p>提供基本的大数字构造</p>
<p>提供数字的四则运算方法</p>
<p>add方法加法，</p>
<p>BigInteger类(整数)</p>
<h2 id="4-包装类"><a href="#4-包装类" class="headerlink" title="4.包装类"></a>4.包装类</h2><h3 id="1-值类型-x2F-基本数据类型"><a href="#1-值类型-x2F-基本数据类型" class="headerlink" title="[1] 值类型/基本数据类型"></a>[1] 值类型/基本数据类型</h3><p>byte —  Byte  </p>
<p>short —   Short  </p>
<p>int —  Integer  </p>
<p>long —  Long  </p>
<p>char —  Float  </p>
<p>double —  Double  </p>
<p>boolean —  Boolean  </p>
<p>每个值类型后面都有一个引用类型支持，叫包装类(引用数据类型)</p>
<p>包装类在实例化的时候可以直接使用值类型（值类型转包装类（隐藏的））</p>
<p>值类型转包装类，目的是为获得包装类方法的支持</p>
<p>包装类的equals方法比较的是字面值</p>
<pre class=" language-java"><code class="language-java"><span class="token keyword">int</span> a<span class="token operator">=</span><span class="token number">10000</span><span class="token punctuation">;</span>
<span class="token keyword">int</span> b<span class="token operator">=</span><span class="token number">10000</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">//值类型转换包装类，是隐藏手段（这里的隐藏指的是java语法）</span>
Integer a1<span class="token operator">=</span><span class="token number">10000</span><span class="token punctuation">;</span>
Integer b1<span class="token operator">=</span><span class="token number">10000</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">//值类型比较内容是否相等使用==</span>
System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>a<span class="token operator">==</span>b<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">//引用类型比较内容是否相等不能使用==</span>
System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>a1<span class="token punctuation">.</span><span class="token function">equals</span><span class="token punctuation">(</span>b1<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">//引用类型使用==比较的是地址</span>
System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>a1<span class="token operator">==</span>b1<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>
<h3 id="2-装箱拆箱"><a href="#2-装箱拆箱" class="headerlink" title="[2]装箱拆箱"></a>[2]装箱拆箱</h3><p>装箱</p>
<p>值类型转换成包装类型</p>
<p>目的：使用包装类的方法，但只能使用equals比较内容，使用==比较内存地址</p>
<p>隐含：实际上是使用了包装类的静态方法valueOf()方法</p>
<p>拆箱</p>
<p>包装类型转换成值类型</p>
<p>目的：不使用包装类的方法，且可以使用==比较内容</p>
<p>隐含：对象方法xx.value()  (例如整型拆箱使用intValue()）进行拆箱</p>
<h3 id="3-包装类的缓存方案"><a href="#3-包装类的缓存方案" class="headerlink" title="[3] 包装类的缓存方案"></a>[3] 包装类的缓存方案</h3><p>目的：节约创建对象的数量，优化的目的</p>
<p>如果使用new关键字那么必然会创建对象</p>
<p>装箱的时候(调用valueOf)具备优化条件</p>
<p>​	所有整数，创建了数组存了256个数作为缓存区间</p>
<p>​	小数没有缓存区间</p>
<p>​	布尔类型没有缓存区间</p>
<h1 id="三、集合"><a href="#三、集合" class="headerlink" title="三、集合"></a>三、集合</h1><h2 id="1-集合架构"><a href="#1-集合架构" class="headerlink" title="1. 集合架构"></a>1. 集合架构</h2><h3 id="1-集合概念"><a href="#1-集合概念" class="headerlink" title="[1] 集合概念"></a>[1] 集合概念</h3><ul>
<li>集合也是容器<ul>
<li>集合元素是任意数据类型（这也是弱点，泛型可以固定一种类型）<ul>
<li>泛型的语法是&lt;数据类型&gt;</li>
</ul>
</li>
<li>集合长度任意</li>
<li>集合元素必须全部都是引用类型</li>
</ul>
</li>
<li>集合在java.util包中</li>
<li>Collection接口定义了一些基本的集合操作方法<ul>
<li>集合Collection接口继承了Iterable，因此具备迭代能力</li>
<li>Collection接口拥有子接口List接口<ul>
<li>有下标</li>
<li>可重复</li>
<li>有序</li>
</ul>
</li>
</ul>
</li>
</ul>
<h3 id="2-ArrayList"><a href="#2-ArrayList" class="headerlink" title="[2] ArrayList"></a>[2] ArrayList</h3><ul>
<li>利用数组实现的List集合<ul>
<li>数组是固定数据类型<ul>
<li>数组是Object数据类型</li>
</ul>
</li>
<li>数组是固定长度<ul>
<li>使用数组扩容方案</li>
</ul>
</li>
</ul>
</li>
<li>ArrayList特点<ul>
<li>拥有下标</li>
<li>集合内容可以重复</li>
<li>集合为有序集合</li>
</ul>
</li>
</ul>
<h3 id="3-ArrayList操作"><a href="#3-ArrayList操作" class="headerlink" title="[3] ArrayList操作"></a>[3] ArrayList操作</h3><p>构造</p>
<ul>
<li>带初始容量参数构造</li>
<li>空构造</li>
<li>带一个集合构造（相当于集合拷贝）</li>
</ul>
<p>添加</p>
<ul>
<li>add<ul>
<li>末尾追加</li>
<li>插入</li>
</ul>
</li>
<li>addAll</li>
</ul>
<p>删除</p>
<ul>
<li>remove<ul>
<li>参数为下标</li>
<li>参数为元素</li>
</ul>
</li>
<li>removeAll</li>
<li>clear</li>
</ul>
<p>修改</p>
<ul>
<li>set</li>
</ul>
<p>查询（按条件）</p>
<ul>
<li>get（给定下标）</li>
<li>indexOf（给定内容）</li>
<li>lastIndexOf（给定内容）</li>
</ul>
<p>遍历（查询所有）</p>
<ul>
<li>for循环遍历</li>
<li>foreach循环</li>
<li>迭代器</li>
<li>List迭代器</li>
</ul>
<p>排序（JDK1.8支持）</p>
<ul>
<li>sort<ul>
<li>集合元素类自己实现Comparable接口达到内部排序规则的目的<ul>
<li>弱点在于无法修改</li>
<li>sort(null)参数为null</li>
</ul>
</li>
<li>创建新类实现Comparator接口达到外部排序规则的目的<ul>
<li>弱点，类多（一个规则一个类）</li>
<li>sort(外部排序类)</li>
</ul>
</li>
<li>外部排序（Comparator）优先级高于内部排序（Comparable）</li>
</ul>
</li>
</ul>
<p>面试：微服务主要五个框架，MySQL索引，hash表</p>
<h3 id="4-集合基本信息"><a href="#4-集合基本信息" class="headerlink" title="[4] 集合基本信息"></a>[4] 集合基本信息</h3><ul>
<li>集合在java.util包中</li>
<li>集合拥有两大接口Collection、Map</li>
<li>Collections是集合工具类</li>
<li>集合是容器</li>
<li>集合元素是任意数据类型</li>
<li>集合利用泛型固定数据类型</li>
<li>集合长度任意</li>
<li>集合元素必须全部都是引用类型</li>
</ul>
<h2 id="2-Collection接口"><a href="#2-Collection接口" class="headerlink" title="2. Collection接口"></a>2. Collection接口</h2><h3 id="基础信息"><a href="#基础信息" class="headerlink" title="基础信息"></a><strong>基础信息</strong></h3><ul>
<li>Collection接口定义了一些基本的集合操作方法</li>
<li>集合Collection接口继承了Iterable，因此具备迭代能力</li>
<li>Collection子接口<ul>
<li>List接口（可重复，有序）【数组实现、链表实现，序（自然序、升降序）】</li>
<li>Set接口（不可重复，无序）【哈希表（数组+链表+红黑），序（无序、升降序、自然序）】</li>
<li>Deque接口（可重复，堆栈、队列数据结构）【数组实现、链表实现、（先进先出、先进后出）】</li>
</ul>
</li>
</ul>
<h3 id="结构图"><a href="#结构图" class="headerlink" title="结构图"></a>结构图</h3><p><img src="" alt="图片"></p>
<h2 id="3-List接口"><a href="#3-List接口" class="headerlink" title="3. List接口"></a>3. List接口</h2><h3 id="基本信息"><a href="#基本信息" class="headerlink" title="基本信息"></a><strong>基本信息</strong></h3><ul>
<li>线性数据结构</li>
<li>特点<ul>
<li>可重复</li>
<li>有序</li>
</ul>
</li>
<li>子类<ul>
<li>数组方案ArrayList（线程不安全）、Vector（线程安全）</li>
<li>链表方案LinkedList</li>
</ul>
</li>
</ul>
<h3 id="结构图-1"><a href="#结构图-1" class="headerlink" title="结构图"></a>结构图</h3><p><img src="" alt="图片"></p>
<h2 id="4-Deque（父接口Queue）"><a href="#4-Deque（父接口Queue）" class="headerlink" title="4. Deque（父接口Queue）"></a>4. Deque（父接口Queue）</h2><h3 id="基本信息-1"><a href="#基本信息-1" class="headerlink" title="基本信息"></a>基本信息</h3><ul>
<li>双端队列数据结构</li>
<li>同时可以是堆栈数据结构</li>
<li>特点<ul>
<li>可重复</li>
<li>有序</li>
</ul>
</li>
<li>子类<ul>
<li>数组方案ArrayDeque</li>
<li>链表方案LinkedList</li>
</ul>
</li>
</ul>
<h4 id="结构图-2"><a href="#结构图-2" class="headerlink" title="结构图"></a>结构图</h4><p><img src="" alt="图片"></p>
<h2 id="5-Set接口"><a href="#5-Set接口" class="headerlink" title="5. Set接口"></a>5. Set接口</h2><h3 id="基本信息-2"><a href="#基本信息-2" class="headerlink" title="基本信息"></a>基本信息</h3><ul>
<li>hash、Tree数据结构</li>
<li>特点<ul>
<li>不可重复（利用hashCode、equals判定重复）</li>
<li>无序（Tree结构有序）</li>
</ul>
</li>
<li>子类<ul>
<li>hash方案HaseSet</li>
<li>tree方案TreeSet</li>
<li>自然顺序LinkedHashSet</li>
</ul>
</li>
</ul>
<h3 id="结构图-3"><a href="#结构图-3" class="headerlink" title="结构图"></a>结构图</h3><p><img src="" alt="图片"></p>
<h2 id="6-ArrayList子类"><a href="#6-ArrayList子类" class="headerlink" title="6. ArrayList子类"></a>6. ArrayList子类</h2><h3 id="基础信息-1"><a href="#基础信息-1" class="headerlink" title="基础信息"></a>基础信息</h3><ul>
<li>使用数组扩容改变数组固定长度<ul>
<li>初始化容量: 10</li>
<li>扩容: 1.5倍</li>
</ul>
</li>
<li>线程不安全</li>
<li>ArrayList优势<ul>
<li>自然序</li>
<li>寻址容易（下标）</li>
</ul>
</li>
<li>ArrayList弱点<ul>
<li>插入和删除困难（下标调整、扩容）</li>
</ul>
</li>
<li>ArrayList适合场景<ul>
<li>查询</li>
</ul>
</li>
</ul>
<h2 id="7-ArrayList操作"><a href="#7-ArrayList操作" class="headerlink" title="7. ArrayList操作"></a>7. ArrayList操作</h2><h3 id="构造"><a href="#构造" class="headerlink" title="构造"></a>构造</h3><ul>
<li>带初始容量参数构造</li>
<li>空构造</li>
<li>带一个集合构造（相当于集合拷贝）</li>
</ul>
<h3 id="添加"><a href="#添加" class="headerlink" title="添加"></a>添加</h3><ul>
<li>add<ul>
<li>末尾追加</li>
<li>插入</li>
</ul>
</li>
<li>addAll</li>
</ul>
<h3 id="删除"><a href="#删除" class="headerlink" title="删除"></a>删除</h3><ul>
<li>remove<ul>
<li>参数为下标</li>
<li>参数为元素</li>
</ul>
</li>
<li>removeAll</li>
<li>clear</li>
</ul>
<h3 id="修改"><a href="#修改" class="headerlink" title="修改"></a>修改</h3><ul>
<li>set</li>
</ul>
<h3 id="查询（按条件）"><a href="#查询（按条件）" class="headerlink" title="查询（按条件）"></a>查询（按条件）</h3><ul>
<li>get（给定下标）</li>
<li>indexOf（给定内容）</li>
<li>lastIndexOf（给定内容）</li>
</ul>
<h3 id="遍历（查询所有）"><a href="#遍历（查询所有）" class="headerlink" title="遍历（查询所有）"></a>遍历（查询所有）</h3><ul>
<li>for循环遍历</li>
<li>foreach循环</li>
<li>迭代器</li>
<li>List迭代器</li>
</ul>
<h3 id="排序（JDK1-8支持）"><a href="#排序（JDK1-8支持）" class="headerlink" title="排序（JDK1.8支持）"></a>排序（JDK1.8支持）</h3><ul>
<li>sort<ul>
<li>集合元素类自己实现Comparable接口达到内部排序规则的目的<ul>
<li>弱点在于无法修改</li>
<li>sort(null)参数为null</li>
</ul>
</li>
<li>创建新类实现Comparator接口达到外部排序规则的目的<ul>
<li>弱点，类多（一个规则一个类）</li>
<li>sort(外部排序类)</li>
</ul>
</li>
<li>外部排序（Comparator）优先级高于内部排序（Comparable）</li>
</ul>
</li>
</ul>
<h2 id="8-Vector子类"><a href="#8-Vector子类" class="headerlink" title="8. Vector子类"></a>8. Vector子类</h2><h3 id="基础信息-2"><a href="#基础信息-2" class="headerlink" title="基础信息"></a>基础信息</h3><ul>
<li>使用数组扩容改变数组固定长度<ul>
<li>初始化容量: 10</li>
<li>扩容: 2倍</li>
</ul>
</li>
<li>线程安全</li>
<li>操作与ArrayList一样</li>
</ul>
<h2 id="9-LinkedList子类"><a href="#9-LinkedList子类" class="headerlink" title="9. LinkedList子类"></a>9. LinkedList子类</h2><h3 id="基础信息-3"><a href="#基础信息-3" class="headerlink" title="基础信息"></a>基础信息</h3><ul>
<li>链表实现List、Deque接口</li>
<li>LinkedList优势<ul>
<li>可以队列</li>
<li>可以堆栈</li>
<li>可以线性</li>
<li>插入和删除容易</li>
</ul>
</li>
<li>LinkedList弱点<ul>
<li>寻址困难（无下标）</li>
</ul>
</li>
<li>LinkedList使用场景<ul>
<li>队列</li>
<li>堆栈</li>
<li>增加、删除</li>
</ul>
</li>
</ul>
<h2 id="10-HastSet子类"><a href="#10-HastSet子类" class="headerlink" title="10. HastSet子类"></a>10. HastSet子类</h2><h3 id="基本信息-3"><a href="#基本信息-3" class="headerlink" title="基本信息"></a>基本信息</h3><ul>
<li>底层是HashMap<ul>
<li>需要hashCode与equals都相等则视为相等</li>
</ul>
</li>
<li>线程不安全</li>
<li>HashSet优势<ul>
<li>无重复</li>
</ul>
</li>
<li>HashSet弱点<ul>
<li>无序（遍历顺序不确定）</li>
</ul>
</li>
<li>HashSet使用场景<ul>
<li>点赞</li>
<li>投票</li>
<li>去重</li>
</ul>
</li>
</ul>
<h2 id="11-TreeSet子类"><a href="#11-TreeSet子类" class="headerlink" title="11. TreeSet子类"></a>11. TreeSet子类</h2><h3 id="基本信息-4"><a href="#基本信息-4" class="headerlink" title="基本信息"></a>基本信息</h3><ul>
<li>底层TreeMap</li>
<li>Comparator、Comparable接口判定相等实现无重复</li>
<li>排序使用TreeMap的红黑树算法</li>
<li>线程不安全</li>
<li>TreeSet优势<ul>
<li>无重复</li>
<li>升降序（Comparator、Comparable接口判定排序依据）</li>
</ul>
</li>
<li>TreeSet弱点<ul>
<li>升降序消耗性能</li>
</ul>
</li>
<li>TreeSet使用场景<ul>
<li>去重</li>
<li>升降序</li>
</ul>
</li>
</ul>
<h2 id="12-Map接口"><a href="#12-Map接口" class="headerlink" title="12. Map接口"></a>12. Map接口</h2><h3 id="基本信息-5"><a href="#基本信息-5" class="headerlink" title="基本信息"></a>基本信息</h3><ul>
<li>键值对</li>
<li>特点<ul>
<li>建为Set集合</li>
<li>值为List集合</li>
</ul>
</li>
<li>子类<ul>
<li>HashMap</li>
<li>LinkedHashMap</li>
<li>TreeMap</li>
<li>ConcurrentHashMap（线程安全）</li>
</ul>
</li>
</ul>
<h2 id="13-HashMap子类"><a href="#13-HashMap子类" class="headerlink" title="13. HashMap子类"></a>13. HashMap子类</h2><h3 id="基本信息-6"><a href="#基本信息-6" class="headerlink" title="基本信息"></a>基本信息</h3><ul>
<li>哈希表（数组+链表+红黑树）【结合了数组和链表的优点】数据结构</li>
<li>线程不安全</li>
<li>HashMap优点<ul>
<li>无序</li>
<li>查询、增、删效率高</li>
</ul>
</li>
<li>HashMap弱点<ul>
<li>遍历顺序不确定</li>
</ul>
</li>
</ul>
<h2 id="14-LinkedHashMap子类"><a href="#14-LinkedHashMap子类" class="headerlink" title="14. LinkedHashMap子类"></a>14. LinkedHashMap子类</h2><h3 id="基本信息-7"><a href="#基本信息-7" class="headerlink" title="基本信息"></a>基本信息</h3><ul>
<li>数组+链表+红黑树数据结构</li>
<li>线程不安全</li>
<li>LinkedHashMap优点<ul>
<li>自然序</li>
<li>查询效率高</li>
</ul>
</li>
<li>LinkedHashMap弱点<ul>
<li>效率略低</li>
</ul>
</li>
</ul>
<h2 id="15-TreeMap子类"><a href="#15-TreeMap子类" class="headerlink" title="15. TreeMap子类"></a>15. TreeMap子类</h2><h3 id="基本信息-8"><a href="#基本信息-8" class="headerlink" title="基本信息"></a>基本信息</h3><ul>
<li>红黑树数据结构</li>
<li>线程不安全</li>
<li>TreeMap优点<ul>
<li>升降序（Comparator、Comparable接口判定排序依据）</li>
</ul>
</li>
<li>TreeMap弱点<ul>
<li>效率略低</li>
</ul>
</li>
</ul>
<h1 id="四、异常"><a href="#四、异常" class="headerlink" title="四、异常"></a>四、异常</h1><h2 id="1-异常的分类"><a href="#1-异常的分类" class="headerlink" title="1.异常的分类"></a>1.异常的分类</h2><ul>
<li>Throwable为异常的父类<ul>
<li>Error表示程序无法处理</li>
<li>Exception表示程序可以处理<ul>
<li>Exception一般异常</li>
<li>RuntimeException运行时异常</li>
</ul>
</li>
</ul>
</li>
</ul>
<p><img src="" alt="图片"></p>
<h2 id="2-异常的处理流程"><a href="#2-异常的处理流程" class="headerlink" title="2.异常的处理流程"></a>2.异常的处理流程</h2><ul>
<li>一般异常不处理，直接无法编译</li>
<li>运行时异常不出处理，如果不触发异常没事，触发异常程序中断</li>
<li>处理<ul>
<li>第一：捕获（try范围）</li>
<li>第二：处理（catch(异常类型)范围）</li>
<li>第三：扫尾（finally范围）（可以不写，但是一旦写出来，必执行）</li>
</ul>
</li>
<li>好处：<ul>
<li>程序不会因为异常而中断</li>
</ul>
</li>
</ul>
<h2 id="3-异常的基本操作"><a href="#3-异常的基本操作" class="headerlink" title="3.异常的基本操作"></a>3.异常的基本操作</h2><p>基本的操作（但是太多try）</p>
<pre class=" language-java"><code class="language-java"><span class="token keyword">try</span> <span class="token punctuation">{</span>
    <span class="token comment" spellcheck="true">//捕获（不一定以引发）</span>
    String str <span class="token operator">=</span> null<span class="token punctuation">;</span>
    System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>str<span class="token punctuation">.</span><span class="token function">length</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">Exception</span> e<span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token comment" spellcheck="true">//处理</span>
    System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"字符串是空的..."</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token keyword">try</span> <span class="token punctuation">{</span>
    <span class="token keyword">int</span> a<span class="token operator">=</span><span class="token number">10</span><span class="token punctuation">;</span>
    System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>a<span class="token operator">/</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">Exception</span> e<span class="token punctuation">)</span><span class="token punctuation">{</span>
    System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"除数不能是0"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token keyword">try</span> <span class="token punctuation">{</span>
    <span class="token keyword">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span> b<span class="token operator">=</span> <span class="token punctuation">{</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">}</span><span class="token punctuation">;</span>
    System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>b<span class="token punctuation">[</span><span class="token number">10</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">Exception</span> e<span class="token punctuation">)</span><span class="token punctuation">{</span>
    System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"数组超标啦"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"今天还可以..........."</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>
<p>多路catch</p>
<pre class=" language-java"><code class="language-java"><span class="token keyword">try</span> <span class="token punctuation">{</span>
    <span class="token comment" spellcheck="true">//捕获（不一定以引发）</span>
    String str <span class="token operator">=</span> null<span class="token punctuation">;</span>
    System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>str<span class="token punctuation">.</span><span class="token function">length</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">int</span> a<span class="token operator">=</span><span class="token number">10</span><span class="token punctuation">;</span>
    System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>a<span class="token operator">/</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span> b<span class="token operator">=</span> <span class="token punctuation">{</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">}</span><span class="token punctuation">;</span>
    System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>b<span class="token punctuation">[</span><span class="token number">10</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">NullPointerException</span> e<span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token comment" spellcheck="true">//处理空指针</span>
    System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"字符串是空的..."</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">ArithmeticException</span> e<span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token comment" spellcheck="true">//处理除0</span>
    System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"不可除0"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">ArrayIndexOutOfBoundsException</span> e<span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token comment" spellcheck="true">//处理数组超标</span>
    System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"数组超标"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">Exception</span> e<span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token comment" spellcheck="true">//处理（兜底）</span>
    System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"反正报错了，也不知道什么错"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>                
System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"今天还可以..........."</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>
<p>finally</p>
<pre class=" language-java"><code class="language-java"><span class="token keyword">try</span> <span class="token punctuation">{</span>
    String str <span class="token operator">=</span> null<span class="token punctuation">;</span>
    System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>str<span class="token punctuation">.</span><span class="token function">length</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"aaaaaaaaaaaaaaaaa"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment" spellcheck="true">//碰到exit中断，那么finally就不会执行</span>
    <span class="token comment" spellcheck="true">//System.exit(0);</span>
    <span class="token comment" spellcheck="true">//return关键字一定终止方法</span>
    <span class="token keyword">return</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">Exception</span> e<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"字符串空啦"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span> <span class="token keyword">finally</span> <span class="token punctuation">{</span>
    <span class="token comment" spellcheck="true">//一般在这里做一些关闭（EasyExcel）</span>
    System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"我是扫尾"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"开心呀"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>   
</code></pre>
<p>什么时候用try</p>
<ul>
<li>一般异常</li>
<li>运行时异常<ul>
<li>无法判断用户输入的时候</li>
</ul>
</li>
</ul>
<pre class=" language-java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span>String<span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token comment" spellcheck="true">//输入接收变量</span>
    <span class="token keyword">int</span> i<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span>
    <span class="token comment" spellcheck="true">//避免JVM编译期认为while死循环，导致while后面代码没法执行</span>
    <span class="token comment" spellcheck="true">//boolean b=true;</span>
    <span class="token comment" spellcheck="true">//给三次输入机会</span>
    <span class="token keyword">int</span> js<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">;</span>
    <span class="token keyword">while</span> <span class="token punctuation">(</span>js<span class="token operator">&lt;=</span><span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
        <span class="token keyword">try</span> <span class="token punctuation">{</span>
            <span class="token comment" spellcheck="true">//Scanner每次都需要一个新对象</span>
            Scanner scanner<span class="token operator">=</span><span class="token keyword">new</span> <span class="token class-name">Scanner</span><span class="token punctuation">(</span>System<span class="token punctuation">.</span>in<span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token comment" spellcheck="true">//如果目前没有办法使用if判断那么就使用异常捕获</span>
            i <span class="token operator">=</span> scanner<span class="token punctuation">.</span><span class="token function">nextInt</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token keyword">break</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
        <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">Exception</span> e<span class="token punctuation">)</span><span class="token punctuation">{</span>
            System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"请你输入整数"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            js<span class="token operator">++</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
    System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span><span class="token punctuation">;</span>
    System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"程序结束................"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
<h2 id="4-throws关键字"><a href="#4-throws关键字" class="headerlink" title="4.throws关键字"></a>4.throws关键字</h2><p>throws关键字向上抛出异常</p>
<ul>
<li>向上抛出异常</li>
<li>写在方法上面</li>
<li>DAO层向业务层抛，如果业务层不能处理，向控制层抛。</li>
<li>一般都是抛具体的异常</li>
</ul>
<pre class=" language-java"><code class="language-java"><span class="token comment" spellcheck="true">//如果引发异常，我的业务没办法处理</span>
<span class="token comment" spellcheck="true">//向上抛出异常，由调用本方法的对象处理</span>
<span class="token comment" spellcheck="true">//在方法声明最后使用关键字throws 然后加上异常类型</span>
<span class="token comment" spellcheck="true">//throws可以跟多个异常类</span>
<span class="token comment" spellcheck="true">//throws可以直接使用Exception完事，但是绝对不提倡</span>
<span class="token keyword">public</span> <span class="token keyword">int</span> <span class="token function">fun1</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">throws</span> NullPointerException<span class="token punctuation">,</span> ClassNotFoundException <span class="token punctuation">{</span>
    <span class="token keyword">int</span> length <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
    String str <span class="token operator">=</span> null<span class="token punctuation">;</span>
    <span class="token comment" spellcheck="true">//向上抛运行时异常NullPointerException</span>
    <span class="token comment" spellcheck="true">//这里也无法处理，因此向上抛</span>
    length <span class="token operator">=</span> str<span class="token punctuation">.</span><span class="token function">length</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment" spellcheck="true">//调用了向上抛一般异常ClassNotFoundException的forName方法</span>
    <span class="token comment" spellcheck="true">//这里也无法处理，因此继续向上抛</span>
    Class<span class="token punctuation">.</span><span class="token function">forName</span><span class="token punctuation">(</span><span class="token string">"aaaaa"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">return</span> length<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
<pre class=" language-java"><code class="language-java"><span class="token comment" spellcheck="true">//我们在看别人写的方法的时候一定要看是否向上抛了异常</span>
<span class="token comment" spellcheck="true">//那么你作为方法的调用者，你需要考虑，是否继续向上抛，还是try处理</span>
<span class="token keyword">public</span> <span class="token keyword">static</span> Class<span class="token operator">&lt;</span><span class="token operator">?</span><span class="token operator">></span> <span class="token function">forName</span><span class="token punctuation">(</span>String className<span class="token punctuation">)</span>
            <span class="token keyword">throws</span> ClassNotFoundException <span class="token punctuation">{</span>
    Class<span class="token operator">&lt;</span><span class="token operator">?</span><span class="token operator">></span> caller <span class="token operator">=</span> Reflection<span class="token punctuation">.</span><span class="token function">getCallerClass</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">return</span> <span class="token function">forName0</span><span class="token punctuation">(</span>className<span class="token punctuation">,</span> <span class="token boolean">true</span><span class="token punctuation">,</span> ClassLoader<span class="token punctuation">.</span><span class="token function">getClassLoader</span><span class="token punctuation">(</span>caller<span class="token punctuation">)</span><span class="token punctuation">,</span> caller<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
<h3 id="throw关键字构造异常"><a href="#throw关键字构造异常" class="headerlink" title="throw关键字构造异常"></a>throw关键字构造异常</h3><ul>
<li>throw一般会联合throws，达到调用方处理异常的目的</li>
<li>throw会根据实际情况直接构造一个异常出来</li>
<li>catch可以根据异常类型来做不同的处理，因此throw一个异常对象如果不是JAVA提供的，则可以自定义一个异常类<ul>
<li>自定义异常操作<ul>
<li>继承Exception代表一般异常，必须捕获</li>
<li>继承RuntimeException代表运行时异常，可以不捕获</li>
<li>一定要使用构造方法携参数形式，把异常信息输入进来</li>
</ul>
</li>
</ul>
</li>
</ul>
<pre class=" language-java"><code class="language-java"><span class="token comment" spellcheck="true">//自定义一个30岁异常</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">My30AgeException</span> <span class="token keyword">extends</span>  <span class="token class-name">Exception</span> <span class="token punctuation">{</span>
    <span class="token comment" spellcheck="true">//使用构造方法输入异常信息</span>
    <span class="token keyword">public</span> <span class="token function">My30AgeException</span><span class="token punctuation">(</span>String message<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">super</span><span class="token punctuation">(</span>message<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

<span class="token comment" spellcheck="true">//自定义一个21岁异常</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">My21AgeException</span> <span class="token keyword">extends</span>  <span class="token class-name">Exception</span><span class="token punctuation">{</span>
    <span class="token keyword">public</span> <span class="token function">My21AgeException</span><span class="token punctuation">(</span>String message<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">super</span><span class="token punctuation">(</span>message<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

<span class="token comment" spellcheck="true">//业务类里面的一个方法</span>
<span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">fun3</span><span class="token punctuation">(</span><span class="token keyword">int</span> age<span class="token punctuation">)</span> <span class="token keyword">throws</span> My30AgeException<span class="token punctuation">,</span> My21AgeException <span class="token punctuation">{</span>
    System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"报名"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>age <span class="token operator">>=</span> <span class="token number">30</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token comment" spellcheck="true">//构建了一个异常</span>
        <span class="token keyword">throw</span> <span class="token keyword">new</span> <span class="token class-name">My30AgeException</span><span class="token punctuation">(</span><span class="token string">"30岁搞架构师"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">else</span> <span class="token keyword">if</span><span class="token punctuation">(</span>age <span class="token operator">&lt;=</span> <span class="token number">21</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
        <span class="token keyword">throw</span> <span class="token keyword">new</span> <span class="token class-name">My21AgeException</span><span class="token punctuation">(</span><span class="token string">"21搞学历"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">else</span><span class="token punctuation">{</span>
        System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"上学"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

<span class="token comment" spellcheck="true">//调用业务类的一个代码段</span>
MyServices myServices <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">MyServices</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">try</span> <span class="token punctuation">{</span>
    myServices<span class="token punctuation">.</span><span class="token function">fun3</span><span class="token punctuation">(</span><span class="token number">18</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">My30AgeException</span> e<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"进架构师班级"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">My21AgeException</span> e<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"进学历提升班级"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"程序继续运行..................."</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>
<h1 id="五、IO流"><a href="#五、IO流" class="headerlink" title="五、IO流"></a>五、IO流</h1><p>流     指文件内容</p>
<p>输入 input</p>
<p>输出 output</p>
<p>文件内容</p>
<p>文件可以存在硬盘上，那么流即文件的内容在硬盘上</p>
<p>单位</p>
<p>字节（二进制文件）基本单位</p>
<p>字符（文本文件）2字节</p>
<p>方向</p>
<p>源头—&gt;目的地      输出</p>
<p>目的地—-&gt;源头		输入</p>
<p>功能</p>
<p>节点流（直接连接源头和目的地的）</p>
<p>过滤流（连接节点的）</p>
<p>JavaIO流的架构</p>
<h2 id="File类"><a href="#File类" class="headerlink" title="File类"></a>File类</h2><p>File类代表硬盘中的⽂件或⽂件夹 </p>
<p>File类直接使⽤new关键+⽂件路径实例化对象 </p>
<p>File对象指的是⽂件的属性</p>
<p>File对象基本操作</p>
<ul>
<li>查看⽂件的基本属性，例如⽂件名、⼤⼩、是⽂件夹还是⽂件等</li>
</ul>
<p>创建⽂件或⽂件夹</p>
<p>删除⽂件或⽂件夹</p>
<p>判定⽂件或⽂件夹是否存在</p>
<p>使⽤listFiles⽅法返回⼦⽂件或⼦⽂件夹</p>
<p>通常使⽤递归来获取所有的⽂件或⽂件夹 </p>
<h2 id="IO流"><a href="#IO流" class="headerlink" title="IO流"></a>IO流</h2><p> ⽂件内容</p>
<p>⽂件可以存在于硬盘上，那么流即⽂件的内容在硬盘上（流的源在硬盘上）</p>
<p>流的总量（字节数）是固定的</p>
<p>⽂件可以存在于⽹络上，那么流即⽂件的内容在⽹络上</p>
<p>流的总量（字节数）是不固定的（只有下载完毕后，才真正知道流的总量） </p>
<h2 id="流stream的三个要领（⽂件内容（即流））"><a href="#流stream的三个要领（⽂件内容（即流））" class="headerlink" title="流stream的三个要领（⽂件内容（即流））"></a>流stream的三个要领（⽂件内容（即流））</h2><p> 单位</p>
<p>字节（⼆进制⽂件）（byte）基本单位</p>
<p>InputStream、OutputStream</p>
<p>字符（⽂本⽂件）（char）（等于2字节）</p>
<p>Reader、Writer</p>
<p> ⽅向【Java参照我们的程序】</p>
<p>源头（⽂件内容）-&gt;⽬的地（我们的程序）（输⼊） </p>
<p>InputStream、Reader</p>
<p>⽬的地（我们的程序）-&gt;源头（⽂件内容）（输出） </p>
<p> OutputStream、Writer</p>
<p>功能</p>
<p>节点流（直接连接（源头）和（⽬的地））</p>
<p>FileReader、FileInputStream、FileOutputStream、FileWriter</p>
<p>功能流（过滤）（连接节点的）</p>
<p>缓存功能</p>
<p>BufferedInputStream、BufferedReader、BufferedOutputStream、 BufferedWriter、</p>
<p>字节流转字符流</p>
<p>InputStreamReader、OutputStreamWriter </p>
<p>有关流的操作【⽂件的操作】 </p>
<p>读取⽂件内容【字符】 </p>
<pre class=" language-java"><code class="language-java">        <span class="token comment" spellcheck="true">//字符流读取流程</span>
        String path <span class="token operator">=</span> <span class="token string">"d:\\abc.txt"</span><span class="token punctuation">;</span>
        FileReader fileReader <span class="token operator">=</span> null<span class="token punctuation">;</span>
        <span class="token keyword">try</span> <span class="token punctuation">{</span>
            fileReader <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">FileReader</span><span class="token punctuation">(</span>path<span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token comment" spellcheck="true">//舀水的杯子的大小</span>
            <span class="token keyword">char</span><span class="token punctuation">[</span><span class="token punctuation">]</span> chars <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">char</span><span class="token punctuation">[</span><span class="token number">1024</span> <span class="token operator">*</span> <span class="token number">1024</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
            <span class="token comment" spellcheck="true">//read返回的结果如果是-1代表源读完了</span>
            <span class="token keyword">int</span> read <span class="token operator">=</span> fileReader<span class="token punctuation">.</span><span class="token function">read</span><span class="token punctuation">(</span>chars<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> chars<span class="token punctuation">.</span>length<span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token comment" spellcheck="true">//源还有没有流</span>
            <span class="token comment" spellcheck="true">//System.out.println("read="+read);</span>
            <span class="token keyword">while</span> <span class="token punctuation">(</span>read <span class="token operator">!=</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                String info <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">String</span><span class="token punctuation">(</span>chars<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> read<span class="token punctuation">)</span><span class="token punctuation">;</span>
                System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">print</span><span class="token punctuation">(</span>info<span class="token punctuation">)</span><span class="token punctuation">;</span>
                read <span class="token operator">=</span> fileReader<span class="token punctuation">.</span><span class="token function">read</span><span class="token punctuation">(</span>chars<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> chars<span class="token punctuation">.</span>length<span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">FileNotFoundException</span> e<span class="token punctuation">)</span> <span class="token punctuation">{</span>
            e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">IOException</span> e<span class="token punctuation">)</span> <span class="token punctuation">{</span>
            e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span> <span class="token keyword">finally</span> <span class="token punctuation">{</span>
            <span class="token comment" spellcheck="true">//关闭流</span>
            <span class="token keyword">try</span> <span class="token punctuation">{</span>
                fileReader<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">IOException</span> e<span class="token punctuation">)</span> <span class="token punctuation">{</span>
                e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span>
</code></pre>
<p> 写⼊⽂件内容【字符】  </p>
<pre class=" language-java"><code class="language-java">String path <span class="token operator">=</span> <span class="token string">"d:\\abc.txt"</span><span class="token punctuation">;</span>
        String info <span class="token operator">=</span> <span class="token string">"（央视财经《经济信息联播》）一位老人清早在路边散步时，\n"</span> <span class="token operator">+</span>
                "因为突然受到一声气喇叭的惊吓而倒地身亡。家属认为按喇叭的司机存在重大过错，\n
        <span class="token string">"随即将相关人员告上了法庭，索赔34万余元。"</span><span class="token punctuation">;</span>
        FileWriter fileWriter <span class="token operator">=</span> null<span class="token punctuation">;</span>
        <span class="token keyword">try</span> <span class="token punctuation">{</span>
            <span class="token comment" spellcheck="true">//第二参数代表是否覆盖，默认覆盖，给true代表追加</span>
            fileWriter <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">FileWriter</span><span class="token punctuation">(</span>path<span class="token punctuation">,</span> <span class="token boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            fileWriter<span class="token punctuation">.</span><span class="token function">write</span><span class="token punctuation">(</span>info<span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token comment" spellcheck="true">//输出需要flush()刷新缓存</span>
            fileWriter<span class="token punctuation">.</span><span class="token function">flush</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">IOException</span> e<span class="token punctuation">)</span> <span class="token punctuation">{</span>
            e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span> <span class="token keyword">finally</span> <span class="token punctuation">{</span>
            <span class="token keyword">try</span> <span class="token punctuation">{</span>
                fileWriter<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">IOException</span> e<span class="token punctuation">)</span> <span class="token punctuation">{</span>
                e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span>
</code></pre>
<p> ⽂件拷⻉ </p>
<pre class=" language-java"><code class="language-java"><span class="token comment" spellcheck="true">//拷贝都是用字节流操作</span>
        <span class="token comment" spellcheck="true">//源</span>
        String path1 <span class="token operator">=</span> <span class="token string">"d:\\stu.xls"</span><span class="token punctuation">;</span>
        <span class="token comment" spellcheck="true">//目标</span>
        String path2 <span class="token operator">=</span> <span class="token string">"d:\\stucopy.xls"</span><span class="token punctuation">;</span>
        File file1 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">File</span><span class="token punctuation">(</span>path1<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment" spellcheck="true">//文件先创建</span>
        File file2 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">File</span><span class="token punctuation">(</span>path2<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span>file2<span class="token punctuation">.</span><span class="token function">exists</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token comment" spellcheck="true">//开始创建</span>
            <span class="token keyword">try</span> <span class="token punctuation">{</span>
                <span class="token keyword">boolean</span> newFile <span class="token operator">=</span> file2<span class="token punctuation">.</span><span class="token function">createNewFile</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span>newFile<span class="token punctuation">)</span> <span class="token punctuation">{</span>
                    System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"你没空间了"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                    <span class="token keyword">return</span><span class="token punctuation">;</span>
                <span class="token punctuation">}</span>
            <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">IOException</span> e<span class="token punctuation">)</span> <span class="token punctuation">{</span>
                e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span>
        <span class="token comment" spellcheck="true">//开始拷贝</span>
        <span class="token comment" spellcheck="true">//d:\\stu.xls</span>
        FileInputStream fileInputStream <span class="token operator">=</span> null<span class="token punctuation">;</span>
        <span class="token comment" spellcheck="true">//"d:\\stucopy.xls";</span>
        FileOutputStream fileOutputStream <span class="token operator">=</span> null<span class="token punctuation">;</span>
        <span class="token keyword">try</span> <span class="token punctuation">{</span>
            <span class="token comment" spellcheck="true">//字节输入流创建</span>
            fileInputStream <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">FileInputStream</span><span class="token punctuation">(</span>path1<span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token comment" spellcheck="true">//获得流的总量</span>
            <span class="token comment" spellcheck="true">//fileInputStream.available();</span>
            <span class="token comment" spellcheck="true">//字节输出流创建</span>
            fileOutputStream <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">FileOutputStream</span><span class="token punctuation">(</span>path2<span class="token punctuation">,</span> <span class="token boolean">false</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token comment" spellcheck="true">//水瓢,水瓢越循环次数越少</span>
            <span class="token keyword">byte</span><span class="token punctuation">[</span><span class="token punctuation">]</span> bytes <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">byte</span><span class="token punctuation">[</span><span class="token number">1024</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
            <span class="token comment" spellcheck="true">//读取d:\stu.xls的内容到我们程序的水瓢里面（bytes）</span>
            <span class="token keyword">int</span> read <span class="token operator">=</span> fileInputStream<span class="token punctuation">.</span><span class="token function">read</span><span class="token punctuation">(</span>bytes<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> bytes<span class="token punctuation">.</span>length<span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token comment" spellcheck="true">//read返回的不是-1代表没有读完</span>
            <span class="token keyword">while</span> <span class="token punctuation">(</span>read <span class="token operator">!=</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                <span class="token comment" spellcheck="true">//拷贝,就是这里不System.out.println();</span>
                <span class="token comment" spellcheck="true">//把我们程序的水瓢里面（bytes）数据，输出到d:\\stucopy.xls里面</span>
                fileOutputStream<span class="token punctuation">.</span><span class="token function">write</span><span class="token punctuation">(</span>bytes<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> read<span class="token punctuation">)</span><span class="token punctuation">;</span>
                System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"正在拷贝.................."</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                <span class="token comment" spellcheck="true">//继续读取d:\stu.xls的内容到我们程序的水瓢里面（bytes）</span>
                read <span class="token operator">=</span> fileInputStream<span class="token punctuation">.</span><span class="token function">read</span><span class="token punctuation">(</span>bytes<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> bytes<span class="token punctuation">.</span>length<span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span>
            <span class="token comment" spellcheck="true">//输出有缓存支持，所以输出一定要清空缓存</span>
            fileOutputStream<span class="token punctuation">.</span><span class="token function">flush</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">FileNotFoundException</span> e<span class="token punctuation">)</span> <span class="token punctuation">{</span>
            e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">IOException</span> e<span class="token punctuation">)</span> <span class="token punctuation">{</span>
            e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span> <span class="token keyword">finally</span> <span class="token punctuation">{</span>
            <span class="token keyword">try</span> <span class="token punctuation">{</span>
                fileInputStream<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">IOException</span> e<span class="token punctuation">)</span> <span class="token punctuation">{</span>
                e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span>
            <span class="token keyword">try</span> <span class="token punctuation">{</span>
                fileOutputStream<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">IOException</span> e<span class="token punctuation">)</span> <span class="token punctuation">{</span>
                e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span>
</code></pre>
<p> 缓存功能流【BufferedReader 】 </p>
<pre class=" language-java"><code class="language-java">String path <span class="token operator">=</span> <span class="token string">"d:\\abc.txt"</span><span class="token punctuation">;</span>
        FileReader fileReader <span class="token operator">=</span> null<span class="token punctuation">;</span>
        BufferedReader bufferedReader <span class="token operator">=</span> null<span class="token punctuation">;</span>
        <span class="token keyword">try</span> <span class="token punctuation">{</span>
            fileReader <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">FileReader</span><span class="token punctuation">(</span>path<span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token comment" spellcheck="true">//功能流的特点</span>
            <span class="token comment" spellcheck="true">//字符缓存读取功能流，就使用它的读一行功能</span>
            bufferedReader <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">BufferedReader</span><span class="token punctuation">(</span>fileReader<span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token comment" spellcheck="true">//读一行功能</span>
            String s <span class="token operator">=</span> bufferedReader<span class="token punctuation">.</span><span class="token function">readLine</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
           <span class="token keyword">while</span><span class="token punctuation">(</span>s<span class="token operator">!=</span>null<span class="token punctuation">)</span><span class="token punctuation">{</span>
                System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>s<span class="token punctuation">)</span><span class="token punctuation">;</span>
                s <span class="token operator">=</span> bufferedReader<span class="token punctuation">.</span><span class="token function">readLine</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">FileNotFoundException</span> e<span class="token punctuation">)</span> <span class="token punctuation">{</span>
            e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">IOException</span> e<span class="token punctuation">)</span> <span class="token punctuation">{</span>
            e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span> <span class="token keyword">finally</span> <span class="token punctuation">{</span>
            <span class="token keyword">try</span> <span class="token punctuation">{</span>
                bufferedReader<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">IOException</span> e<span class="token punctuation">)</span> <span class="token punctuation">{</span>
                e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
</code></pre>
<p> 缓存功能流【BufferedWriter】 </p>
<pre class=" language-java"><code class="language-java">        String path <span class="token operator">=</span> <span class="token string">"d:\\hzj.txt"</span><span class="token punctuation">;</span>
        String info <span class="token operator">=</span> <span class="token string">"企业微信-企业在线协同办公文档-免费使用-在线文档"</span><span class="token punctuation">;</span>
        FileWriter fileWriter <span class="token operator">=</span> null<span class="token punctuation">;</span>
        BufferedWriter bufferedWriter <span class="token operator">=</span> null<span class="token punctuation">;</span>
        <span class="token keyword">try</span> <span class="token punctuation">{</span>
            <span class="token comment" spellcheck="true">//fileWriter有缓存，但不能改</span>
            fileWriter <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">FileWriter</span><span class="token punctuation">(</span>path<span class="token punctuation">,</span> <span class="token boolean">false</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token comment" spellcheck="true">//可以自定义缓存</span>
            bufferedWriter <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">BufferedWriter</span><span class="token punctuation">(</span>fileWriter<span class="token punctuation">,</span> <span class="token number">1024</span> <span class="token operator">*</span> <span class="token number">100</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            bufferedWriter<span class="token punctuation">.</span><span class="token function">write</span><span class="token punctuation">(</span>info<span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token comment" spellcheck="true">//新功能newLine()</span>
            bufferedWriter<span class="token punctuation">.</span><span class="token function">newLine</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            bufferedWriter<span class="token punctuation">.</span><span class="token function">write</span><span class="token punctuation">(</span>info<span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token comment" spellcheck="true">//清空缓存</span>
            bufferedWriter<span class="token punctuation">.</span><span class="token function">flush</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">IOException</span> e<span class="token punctuation">)</span> <span class="token punctuation">{</span>
            e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span> <span class="token keyword">finally</span> <span class="token punctuation">{</span>
            <span class="token keyword">try</span> <span class="token punctuation">{</span>
                bufferedWriter<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">IOException</span> e<span class="token punctuation">)</span> <span class="token punctuation">{</span>
                e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span>
</code></pre>
<p> 字节转字符功能流  </p>
<pre class=" language-java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">MyApp12</span> <span class="token punctuation">{</span>
            <span class="token keyword">public</span> InputStream <span class="token function">fun1</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">throws</span> IOException <span class="token punctuation">{</span>
                String path <span class="token operator">=</span> <span class="token string">"d:\\abc.txt"</span><span class="token punctuation">;</span>
                FileInputStream fileInputStream <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">FileInputStream</span><span class="token punctuation">(</span>path<span class="token punctuation">)</span><span class="token punctuation">;</span>
                <span class="token keyword">return</span> fileInputStream<span class="token punctuation">;</span>
            <span class="token punctuation">}</span>

            <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span>String<span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span>
                MyApp12 myApp12 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">MyApp12</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                InputStream inputStream <span class="token operator">=</span> null<span class="token punctuation">;</span>
                InputStreamReader inputStreamReader <span class="token operator">=</span> null<span class="token punctuation">;</span>
                BufferedReader bufferedReader <span class="token operator">=</span> null<span class="token punctuation">;</span>
                <span class="token keyword">try</span> <span class="token punctuation">{</span>
                    inputStream <span class="token operator">=</span> myApp12<span class="token punctuation">.</span><span class="token function">fun1</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                    <span class="token comment" spellcheck="true">//功能流</span>
                    <span class="token comment" spellcheck="true">//字节转字符</span>
                    inputStreamReader <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">InputStreamReader</span><span class="token punctuation">(</span>inputStream<span class="token punctuation">)</span><span class="token punctuation">;</span>
                    <span class="token comment" spellcheck="true">//功能流读行再来</span>
                    bufferedReader <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">BufferedReader</span><span class="token punctuation">(</span>inputStreamReader<span class="token punctuation">)</span><span class="token punctuation">;</span>
                    String str <span class="token operator">=</span> bufferedReader<span class="token punctuation">.</span><span class="token function">readLine</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                    <span class="token keyword">while</span> <span class="token punctuation">(</span>str <span class="token operator">!=</span> null<span class="token punctuation">)</span> <span class="token punctuation">{</span>
                        System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>str<span class="token punctuation">)</span><span class="token punctuation">;</span>
                        str <span class="token operator">=</span> bufferedReader<span class="token punctuation">.</span><span class="token function">readLine</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                    <span class="token punctuation">}</span>
                <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">IOException</span> e<span class="token punctuation">)</span> <span class="token punctuation">{</span>
                    e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                <span class="token punctuation">}</span> <span class="token keyword">finally</span> <span class="token punctuation">{</span>
                    <span class="token keyword">try</span> <span class="token punctuation">{</span>
                        inputStream<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                    <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">IOException</span> e<span class="token punctuation">)</span> <span class="token punctuation">{</span>
                        e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                    <span class="token punctuation">}</span>
                    <span class="token keyword">try</span> <span class="token punctuation">{</span>
                        bufferedReader<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                    <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">IOException</span> e<span class="token punctuation">)</span> <span class="token punctuation">{</span>
                        e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                    <span class="token punctuation">}</span>
                <span class="token punctuation">}</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span>
</code></pre>
<p> ⽂件拷⻉（带缓存功能流） </p>
<pre class=" language-java"><code class="language-java">String path <span class="token operator">=</span> <span class="token string">"d:\\YunJing_ins.exe"</span><span class="token punctuation">;</span>
        String pathCopy <span class="token operator">=</span> <span class="token string">"d:\\YunJing_insCopy.exe"</span><span class="token punctuation">;</span>
        File file <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">File</span><span class="token punctuation">(</span>pathCopy<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>file<span class="token punctuation">.</span><span class="token function">exists</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"文件存在.............."</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token keyword">return</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
        <span class="token keyword">try</span> <span class="token punctuation">{</span>
            <span class="token keyword">boolean</span> newFile <span class="token operator">=</span> file<span class="token punctuation">.</span><span class="token function">createNewFile</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span>newFile<span class="token punctuation">)</span> <span class="token punctuation">{</span>
                System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"创建新文件失败.............."</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                <span class="token keyword">return</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">IOException</span> e<span class="token punctuation">)</span> <span class="token punctuation">{</span>
            e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"创建新文件异常.............."</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token keyword">return</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
        <span class="token comment" spellcheck="true">//把字节转换成字符</span>
        <span class="token comment" spellcheck="true">//InputStreamReader inputStreamReader=new InputStreamReader();</span>
        <span class="token comment" spellcheck="true">//public InputStream fun1(); public Reader fun1();</span>
        <span class="token comment" spellcheck="true">//InputStreamReader inputStreamReader=new InputStreamReader();</span>
        FileInputStream fileInputStream <span class="token operator">=</span> null<span class="token punctuation">;</span>
        BufferedInputStream bufferedInputStream <span class="token operator">=</span> null<span class="token punctuation">;</span>
        FileOutputStream fileOutputStream <span class="token operator">=</span> null<span class="token punctuation">;</span>
        BufferedOutputStream bufferedOutputStream <span class="token operator">=</span> null<span class="token punctuation">;</span>
        <span class="token keyword">try</span> <span class="token punctuation">{</span>
            fileInputStream <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">FileInputStream</span><span class="token punctuation">(</span>path<span class="token punctuation">)</span><span class="token punctuation">;</span>
            bufferedInputStream <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">BufferedInputStream</span><span class="token punctuation">(</span>fileInputStream<span class="token punctuation">,</span> <span class="token number">1024</span> <span class="token operator">*</span> <span class="token number">10</span>
                    fileOutputStream <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">FileOutputStream</span><span class="token punctuation">(</span>pathCopy<span class="token punctuation">)</span><span class="token punctuation">;</span>
            bufferedOutputStream <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">BufferedOutputStream</span><span class="token punctuation">(</span>fileOutputStream<span class="token punctuation">,</span> <span class="token number">1024</span> <span class="token operator">*</span>
            <span class="token keyword">byte</span><span class="token punctuation">[</span><span class="token punctuation">]</span> bytes <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">byte</span><span class="token punctuation">[</span><span class="token number">1024</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
            <span class="token keyword">int</span> read <span class="token operator">=</span> bufferedInputStream<span class="token punctuation">.</span><span class="token function">read</span><span class="token punctuation">(</span>bytes<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> bytes<span class="token punctuation">.</span>length<span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token keyword">while</span> <span class="token punctuation">(</span>read <span class="token operator">!=</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                bufferedOutputStream<span class="token punctuation">.</span><span class="token function">write</span><span class="token punctuation">(</span>bytes<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> read<span class="token punctuation">)</span><span class="token punctuation">;</span>
                System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"正在拷贝..................."</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                read <span class="token operator">=</span> bufferedInputStream<span class="token punctuation">.</span><span class="token function">read</span><span class="token punctuation">(</span>bytes<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> bytes<span class="token punctuation">.</span>length<span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span>
            bufferedOutputStream<span class="token punctuation">.</span><span class="token function">flush</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">FileNotFoundException</span> e<span class="token punctuation">)</span> <span class="token punctuation">{</span>
            e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">IOException</span> e<span class="token punctuation">)</span> <span class="token punctuation">{</span>
            e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span> <span class="token keyword">finally</span> <span class="token punctuation">{</span>
            <span class="token keyword">try</span> <span class="token punctuation">{</span>
                bufferedInputStream<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">IOException</span> e<span class="token punctuation">)</span> <span class="token punctuation">{</span>
                e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span>
            <span class="token keyword">try</span> <span class="token punctuation">{</span>
                bufferedOutputStream<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">IOException</span> e<span class="token punctuation">)</span> <span class="token punctuation">{</span>
                e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span>
</code></pre>
<p> 有关流的操作【对象的操作】【序列化】  </p>
<blockquote>
<p>把对象的属性值存成⽂件<br> 只有字节<br> DataOutputStream、DataInputStream这两个类都是单纯的读取或者获取对象的属性<br> ObjectOutputStream、ObjectInputStream这两个是对象整体的读取或获取<br> 对象的整体的读取或者获取叫序列化<br> 序列化就是把对象（⼀个）存成⽂件，然后利⽤反序列化再吧对象拿出来</p>
</blockquote>
<p> DataOutputStream操作 </p>
<pre class=" language-java"><code class="language-java"> <span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Stu</span> <span class="token punctuation">{</span>
            <span class="token keyword">int</span> id<span class="token punctuation">;</span>
            String name<span class="token punctuation">;</span>
            <span class="token keyword">int</span> age<span class="token punctuation">;</span>
            LocalDate localDate<span class="token punctuation">;</span>

            <span class="token keyword">public</span> <span class="token function">Stu</span><span class="token punctuation">(</span><span class="token keyword">int</span> id<span class="token punctuation">,</span> String name<span class="token punctuation">,</span> <span class="token keyword">int</span> age<span class="token punctuation">,</span> LocalDate localDate<span class="token punctuation">)</span> <span class="token punctuation">{</span>
                <span class="token keyword">this</span><span class="token punctuation">.</span>id <span class="token operator">=</span> id<span class="token punctuation">;</span>
                <span class="token keyword">this</span><span class="token punctuation">.</span>name <span class="token operator">=</span> name<span class="token punctuation">;</span>
                <span class="token keyword">this</span><span class="token punctuation">.</span>age <span class="token operator">=</span> age<span class="token punctuation">;</span>
                <span class="token keyword">this</span><span class="token punctuation">.</span>localDate <span class="token operator">=</span> localDate<span class="token punctuation">;</span>
            <span class="token punctuation">}</span>

            <span class="token keyword">public</span> <span class="token function">Stu</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token punctuation">}</span>

            <span class="token annotation punctuation">@Override</span>
            <span class="token keyword">public</span> String <span class="token function">toString</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                <span class="token keyword">return</span> <span class="token string">"Stu{"</span> <span class="token operator">+</span>
                        <span class="token string">"id="</span> <span class="token operator">+</span> id <span class="token operator">+</span>
                        <span class="token string">", name='"</span> <span class="token operator">+</span> name <span class="token operator">+</span> <span class="token string">'\''</span> <span class="token operator">+</span>
                        <span class="token string">", age="</span> <span class="token operator">+</span> age <span class="token operator">+</span>
                        <span class="token string">", localDate="</span> <span class="token operator">+</span> localDate <span class="token operator">+</span>
                        <span class="token string">'}'</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span>

            <span class="token keyword">public</span> <span class="token keyword">int</span> <span class="token function">getId</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                <span class="token keyword">return</span> id<span class="token punctuation">;</span>
            <span class="token punctuation">}</span>

            <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">setId</span><span class="token punctuation">(</span><span class="token keyword">int</span> id<span class="token punctuation">)</span> <span class="token punctuation">{</span>
                <span class="token keyword">this</span><span class="token punctuation">.</span>id <span class="token operator">=</span> id<span class="token punctuation">;</span>
            <span class="token punctuation">}</span>

            <span class="token keyword">public</span> String <span class="token function">getName</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                <span class="token keyword">return</span> name<span class="token punctuation">;</span>
            <span class="token punctuation">}</span>

            <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">setName</span><span class="token punctuation">(</span>String name<span class="token punctuation">)</span> <span class="token punctuation">{</span>
                <span class="token keyword">this</span><span class="token punctuation">.</span>name <span class="token operator">=</span> name<span class="token punctuation">;</span>
            <span class="token punctuation">}</span>

            <span class="token keyword">public</span> <span class="token keyword">int</span> <span class="token function">getAge</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                <span class="token keyword">return</span> age<span class="token punctuation">;</span>
            <span class="token punctuation">}</span>

            <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">setAge</span><span class="token punctuation">(</span><span class="token keyword">int</span> age<span class="token punctuation">)</span> <span class="token punctuation">{</span>
                <span class="token keyword">this</span><span class="token punctuation">.</span>age <span class="token operator">=</span> age<span class="token punctuation">;</span>
            <span class="token punctuation">}</span>

            <span class="token keyword">public</span> LocalDate <span class="token function">getLocalDate</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                <span class="token keyword">return</span> localDate<span class="token punctuation">;</span>
            <span class="token punctuation">}</span>

            <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">setLocalDate</span><span class="token punctuation">(</span>LocalDate localDate<span class="token punctuation">)</span> <span class="token punctuation">{</span>
                <span class="token keyword">this</span><span class="token punctuation">.</span>localDate <span class="token operator">=</span> localDate<span class="token punctuation">;</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span>
</code></pre>
<pre class=" language-java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">MyApp</span> <span class="token punctuation">{</span>
            <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span>String<span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span>
                Stu stu1 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Stu</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                stu1<span class="token punctuation">.</span><span class="token function">setId</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                stu1<span class="token punctuation">.</span><span class="token function">setName</span><span class="token punctuation">(</span><span class="token string">"hzj"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                stu1<span class="token punctuation">.</span><span class="token function">setAge</span><span class="token punctuation">(</span><span class="token number">18</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                stu1<span class="token punctuation">.</span><span class="token function">setLocalDate</span><span class="token punctuation">(</span>LocalDate<span class="token punctuation">.</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token number">2022</span><span class="token punctuation">,</span> <span class="token number">8</span><span class="token punctuation">,</span> <span class="token number">9</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>stu1<span class="token punctuation">)</span><span class="token punctuation">;</span>
                String path <span class="token operator">=</span> <span class="token string">"d:\\mystu1.hzj"</span><span class="token punctuation">;</span>
                <span class="token comment" spellcheck="true">//节点流</span>
                FileOutputStream fileOutputStream <span class="token operator">=</span> null<span class="token punctuation">;</span>
                <span class="token comment" spellcheck="true">//功能流</span>
                DataOutputStream dataOutputStream <span class="token operator">=</span> null<span class="token punctuation">;</span>
                <span class="token keyword">try</span> <span class="token punctuation">{</span>
                    fileOutputStream <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">FileOutputStream</span><span class="token punctuation">(</span>path<span class="token punctuation">,</span> <span class="token boolean">false</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                    dataOutputStream <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">DataOutputStream</span><span class="token punctuation">(</span>fileOutputStream<span class="token punctuation">)</span><span class="token punctuation">;</span>
                    <span class="token comment" spellcheck="true">//dataOutputStream提供数据类型的写</span>
                    <span class="token comment" spellcheck="true">//注意这里的顺序</span>
                    dataOutputStream<span class="token punctuation">.</span><span class="token function">writeInt</span><span class="token punctuation">(</span>stu1<span class="token punctuation">.</span><span class="token function">getId</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                    dataOutputStream<span class="token punctuation">.</span><span class="token function">writeUTF</span><span class="token punctuation">(</span>stu1<span class="token punctuation">.</span><span class="token function">getName</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                    dataOutputStream<span class="token punctuation">.</span><span class="token function">writeInt</span><span class="token punctuation">(</span>stu1<span class="token punctuation">.</span><span class="token function">getAge</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                    dataOutputStream<span class="token punctuation">.</span><span class="token function">writeUTF</span><span class="token punctuation">(</span>stu1<span class="token punctuation">.</span><span class="token function">getLocalDate</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
                            <span class="token punctuation">.</span><span class="token function">format</span><span class="token punctuation">(</span>DateTimeFormatter<span class="token punctuation">.</span><span class="token function">ofPattern</span><span class="token punctuation">(</span><span class="token string">"yyyy-MM-dd"</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                    dataOutputStream<span class="token punctuation">.</span><span class="token function">flush</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">FileNotFoundException</span> e<span class="token punctuation">)</span> <span class="token punctuation">{</span>
                    e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">IOException</span> e<span class="token punctuation">)</span> <span class="token punctuation">{</span>
                    e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                <span class="token punctuation">}</span> <span class="token keyword">finally</span> <span class="token punctuation">{</span>
                    <span class="token keyword">try</span> <span class="token punctuation">{</span>
                        dataOutputStream<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                    <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">IOException</span> e<span class="token punctuation">)</span> <span class="token punctuation">{</span>
                        e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                    <span class="token punctuation">}</span>
                <span class="token punctuation">}</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span>
</code></pre>
<p> DataInputStream操作  </p>
<pre class=" language-java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">MyApp2</span> <span class="token punctuation">{</span>
            <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span>String<span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span>
                String path <span class="token operator">=</span> <span class="token string">"d:\\mystu1.hzj"</span><span class="token punctuation">;</span>
                FileInputStream fileInputStream <span class="token operator">=</span> null<span class="token punctuation">;</span>
                DataInputStream dataInputStream <span class="token operator">=</span> null<span class="token punctuation">;</span>
                <span class="token keyword">try</span> <span class="token punctuation">{</span>
                    fileInputStream <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">FileInputStream</span><span class="token punctuation">(</span>path<span class="token punctuation">)</span><span class="token punctuation">;</span>
                    dataInputStream <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">DataInputStream</span><span class="token punctuation">(</span>fileInputStream<span class="token punctuation">)</span><span class="token punctuation">;</span>
                    <span class="token comment" spellcheck="true">//怎么进的，就怎么出</span>
                    <span class="token keyword">int</span> id <span class="token operator">=</span> dataInputStream<span class="token punctuation">.</span><span class="token function">readInt</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                    String name <span class="token operator">=</span> dataInputStream<span class="token punctuation">.</span><span class="token function">readUTF</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                    <span class="token keyword">int</span> age <span class="token operator">=</span> dataInputStream<span class="token punctuation">.</span><span class="token function">readInt</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                    String localdate <span class="token operator">=</span> dataInputStream<span class="token punctuation">.</span><span class="token function">readUTF</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                    Stu stu2 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Stu</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                    stu2<span class="token punctuation">.</span><span class="token function">setId</span><span class="token punctuation">(</span>id<span class="token punctuation">)</span><span class="token punctuation">;</span>
                    stu2<span class="token punctuation">.</span><span class="token function">setName</span><span class="token punctuation">(</span>name<span class="token punctuation">)</span><span class="token punctuation">;</span>
                    stu2<span class="token punctuation">.</span><span class="token function">setAge</span><span class="token punctuation">(</span>age<span class="token punctuation">)</span><span class="token punctuation">;</span>
                    stu2<span class="token punctuation">.</span><span class="token function">setLocalDate</span><span class="token punctuation">(</span>LocalDate<span class="token punctuation">.</span><span class="token function">parse</span><span class="token punctuation">(</span>localdate<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                    System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>stu2<span class="token punctuation">)</span><span class="token punctuation">;</span>
                <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">FileNotFoundException</span> e<span class="token punctuation">)</span> <span class="token punctuation">{</span>
                    e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">IOException</span> e<span class="token punctuation">)</span> <span class="token punctuation">{</span>
                    e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                <span class="token punctuation">}</span> <span class="token keyword">finally</span> <span class="token punctuation">{</span>
                    <span class="token keyword">try</span> <span class="token punctuation">{</span>
                        dataInputStream<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                    <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">IOException</span> e<span class="token punctuation">)</span> <span class="token punctuation">{</span>
                        e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                    <span class="token punctuation">}</span>
                <span class="token punctuation">}</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span>
</code></pre>
<p> ClassRoom类实现序列化标记，并且编写序列化版本 </p>
<pre class=" language-java"><code class="language-java"><span class="token comment" spellcheck="true">//Serializable接口是一个标记，代表可以让系统序列化该类</span>
        <span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">ClassRoom</span> <span class="token keyword">implements</span> <span class="token class-name">Serializable</span> <span class="token punctuation">{</span>
            <span class="token comment" spellcheck="true">//序列化是有版本的</span>
            <span class="token keyword">private</span> <span class="token keyword">static</span> <span class="token keyword">final</span> <span class="token keyword">long</span> serialVersionUID <span class="token operator">=</span> <span class="token number">100</span><span class="token punctuation">;</span>
            <span class="token comment" spellcheck="true">//transient禁止序列化修饰符，</span>
            <span class="token comment" spellcheck="true">//transient int id; 这样的化id属性就无法序列化了，也不会反序列</span>
            <span class="token keyword">int</span> id<span class="token punctuation">;</span>
            String name<span class="token punctuation">;</span>

            <span class="token keyword">public</span> <span class="token function">ClassRoom</span><span class="token punctuation">(</span><span class="token keyword">int</span> id<span class="token punctuation">,</span> String name<span class="token punctuation">)</span> <span class="token punctuation">{</span>
                <span class="token keyword">this</span><span class="token punctuation">.</span>id <span class="token operator">=</span> id<span class="token punctuation">;</span>
                <span class="token keyword">this</span><span class="token punctuation">.</span>name <span class="token operator">=</span> name<span class="token punctuation">;</span>
            <span class="token punctuation">}</span>

            <span class="token keyword">public</span> <span class="token function">ClassRoom</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token punctuation">}</span>

            <span class="token annotation punctuation">@Override</span>
            <span class="token keyword">public</span> String <span class="token function">toString</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                <span class="token keyword">return</span> <span class="token string">"ClassRoom{"</span> <span class="token operator">+</span>
                        <span class="token string">"id="</span> <span class="token operator">+</span> id <span class="token operator">+</span>
                        <span class="token string">", name='"</span> <span class="token operator">+</span> name <span class="token operator">+</span> <span class="token string">'\''</span> <span class="token operator">+</span>
                        <span class="token string">'}'</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span>

            <span class="token keyword">public</span> <span class="token keyword">int</span> <span class="token function">getId</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                <span class="token keyword">return</span> id<span class="token punctuation">;</span>
            <span class="token punctuation">}</span>

            <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">setId</span><span class="token punctuation">(</span><span class="token keyword">int</span> id<span class="token punctuation">)</span> <span class="token punctuation">{</span>
                <span class="token keyword">this</span><span class="token punctuation">.</span>id <span class="token operator">=</span> id<span class="token punctuation">;</span>
            <span class="token punctuation">}</span>

            <span class="token keyword">public</span> String <span class="token function">getName</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                <span class="token keyword">return</span> name<span class="token punctuation">;</span>
            <span class="token punctuation">}</span>

            <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">setName</span><span class="token punctuation">(</span>String name<span class="token punctuation">)</span> <span class="token punctuation">{</span>
                <span class="token keyword">this</span><span class="token punctuation">.</span>name <span class="token operator">=</span> name<span class="token punctuation">;</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span>
</code></pre>
<p> ObjectOutputStream操作 </p>
<pre class=" language-java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">MyApp3</span> <span class="token punctuation">{</span>
            <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span>String<span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span>
                ClassRoom classRoom1 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ClassRoom</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                classRoom1<span class="token punctuation">.</span><span class="token function">setId</span><span class="token punctuation">(</span><span class="token number">1001</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                classRoom1<span class="token punctuation">.</span><span class="token function">setName</span><span class="token punctuation">(</span><span class="token string">"2109"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                String path <span class="token operator">=</span> <span class="token string">"d:\\classroom.hzj"</span><span class="token punctuation">;</span>
                FileOutputStream fileOutputStream <span class="token operator">=</span> null<span class="token punctuation">;</span>
                ObjectOutputStream objectOutputStream <span class="token operator">=</span> null<span class="token punctuation">;</span>
                <span class="token keyword">try</span> <span class="token punctuation">{</span>
                    fileOutputStream <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">FileOutputStream</span><span class="token punctuation">(</span>path<span class="token punctuation">,</span> <span class="token boolean">false</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                    objectOutputStream <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ObjectOutputStream</span><span class="token punctuation">(</span>fileOutputStream<span class="token punctuation">)</span><span class="token punctuation">;</span>
                    <span class="token comment" spellcheck="true">//writeObject新功能</span>
                    <span class="token comment" spellcheck="true">//序列化【把对象存起来】</span>
                    <span class="token comment" spellcheck="true">//java.io.NotSerializableException</span>
                    <span class="token comment" spellcheck="true">//被序列化的类必须要实现一个接口Serializable</span>
                    <span class="token comment" spellcheck="true">//序列化是有版本号的，如果不写会默认有一个，但是这里是强调要写。</span>
                    <span class="token comment" spellcheck="true">//private static final long serialVersionUID = 100;</span>
                    objectOutputStream<span class="token punctuation">.</span><span class="token function">writeObject</span><span class="token punctuation">(</span>classRoom1<span class="token punctuation">)</span><span class="token punctuation">;</span>
                    objectOutputStream<span class="token punctuation">.</span><span class="token function">flush</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">FileNotFoundException</span> e<span class="token punctuation">)</span> <span class="token punctuation">{</span>
                    e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">IOException</span> e<span class="token punctuation">)</span> <span class="token punctuation">{</span>
                    e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                <span class="token punctuation">}</span> <span class="token keyword">finally</span> <span class="token punctuation">{</span>
                    <span class="token keyword">try</span> <span class="token punctuation">{</span>
                        objectOutputStream<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                    <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">IOException</span> e<span class="token punctuation">)</span> <span class="token punctuation">{</span>
                        e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                    <span class="token punctuation">}</span>
                <span class="token punctuation">}</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span>
</code></pre>
<p> ObjectInputStream操作 </p>
<pre class=" language-java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">MyApp4</span> <span class="token punctuation">{</span>
            <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span>String<span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span>
                String path <span class="token operator">=</span> <span class="token string">"d:\\classroom.hzj"</span><span class="token punctuation">;</span>
                FileInputStream fileInputStream <span class="token operator">=</span> null<span class="token punctuation">;</span>
                ObjectInputStream objectInputStream <span class="token operator">=</span> null<span class="token punctuation">;</span>
                <span class="token keyword">try</span> <span class="token punctuation">{</span>
                    fileInputStream <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">FileInputStream</span><span class="token punctuation">(</span>path<span class="token punctuation">)</span><span class="token punctuation">;</span>
                    objectInputStream <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ObjectInputStream</span><span class="token punctuation">(</span>fileInputStream<span class="token punctuation">)</span><span class="token punctuation">;</span>
                    <span class="token comment" spellcheck="true">//反序列化</span>
                    Object o <span class="token operator">=</span> objectInputStream<span class="token punctuation">.</span><span class="token function">readObject</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                    ClassRoom classRoom <span class="token operator">=</span> <span class="token punctuation">(</span>ClassRoom<span class="token punctuation">)</span> o<span class="token punctuation">;</span>
                    System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>classRoom<span class="token punctuation">)</span><span class="token punctuation">;</span>
                <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">FileNotFoundException</span> e<span class="token punctuation">)</span> <span class="token punctuation">{</span>
                    e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">IOException</span> e<span class="token punctuation">)</span> <span class="token punctuation">{</span>
                    e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">ClassNotFoundException</span> e<span class="token punctuation">)</span> <span class="token punctuation">{</span>
                    e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                <span class="token punctuation">}</span> <span class="token keyword">finally</span> <span class="token punctuation">{</span>
                    <span class="token keyword">try</span> <span class="token punctuation">{</span>
                        objectInputStream<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                    <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">IOException</span> e<span class="token punctuation">)</span> <span class="token punctuation">{</span>
                        e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                    <span class="token punctuation">}</span>
                <span class="token punctuation">}</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span>
</code></pre>
<h1 id="六、反射"><a href="#六、反射" class="headerlink" title="六、反射"></a>六、反射</h1><h2 id="反射的概念"><a href="#反射的概念" class="headerlink" title="反射的概念"></a>反射的概念</h2><p> Java中⽤Class类代表类的结构 </p>
<p>Class类的⼀个对象就是反射对象</p>
<p>Class类的⼀个对象就是某⼀个类的结构描述</p>
<p>Class类如何构造 </p>
<p> 静态⽅法forName</p>
<p>类.class</p>
<p>象.getClass() </p>
<h2 id="反射的操作"><a href="#反射的操作" class="headerlink" title="反射的操作"></a>反射的操作</h2><ul>
<li>查看类信息（查看类的结构） <ul>
<li><p>只能访问修饰符允许访问的范围getxxx();</p>
</li>
<li><p>可以访问任意修饰符范围的getDeclaredxxx();</p>
</li>
</ul>
</li>
</ul>
<p>类的结构⼀般分为以下⼏种 </p>
<ul>
<li><p>Field字段类 </p>
</li>
<li><p>getDeclaredFields()获取所有字段 </p>
</li>
<li><p> Method⽅法类 </p>
<ul>
<li>getDeclaredMethods()获取所有⽅法</li>
</ul>
</li>
<li><p>  Parameter⽅法形参类 </p>
<ul>
<li>getParameters()获取所有形参</li>
</ul>
</li>
<li><p>  Constructor构造⽅法类 </p>
<ul>
<li>getDeclaredConstructors()获取所有构造⽅法</li>
</ul>
</li>
<li><p>  Annotation注解类 </p>
<ul>
<li>getAnnotations()获取所有注解</li>
</ul>
</li>
</ul>
<pre class=" language-java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">MyApp</span> <span class="token punctuation">{</span>
            <span class="token comment" spellcheck="true">//Class对象是什么反射对象（Class对象是什么某个类定义的结构）</span>
            <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span>String<span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span>
                <span class="token comment" spellcheck="true">//反射创建对象</span>
                <span class="token comment" spellcheck="true">//任何一个类都有一个反射类Class</span>
                <span class="token comment" spellcheck="true">//创建一个反射类的对象，即创建一个Class类的对象</span>
                <span class="token comment" spellcheck="true">//反射类对象的构造，使用静态方法</span>
                <span class="token keyword">try</span> <span class="token punctuation">{</span>
                    String classSrc <span class="token operator">=</span> <span class="token string">"com.bjpowernode.beans.Stu"</span><span class="token punctuation">;</span>
                    Class <span class="token class-name">stuClass</span> <span class="token operator">=</span> Class<span class="token punctuation">.</span><span class="token function">forName</span><span class="token punctuation">(</span>classSrc<span class="token punctuation">)</span><span class="token punctuation">;</span>
                    <span class="token comment" spellcheck="true">//返回类中字段信息</span>
                    <span class="token comment" spellcheck="true">//stuClass.getxxx();只能访问修饰符允许访问的范围</span>
                    <span class="token comment" spellcheck="true">//stuClass.getDeclaredxxx();可以访问任意修饰符范围的</span>
                    <span class="token comment" spellcheck="true">//Field[] fields = stuClass.getFields();</span>
                    <span class="token comment" spellcheck="true">//查看所有字段</span>
                    Field<span class="token punctuation">[</span><span class="token punctuation">]</span> fields <span class="token operator">=</span> stuClass<span class="token punctuation">.</span><span class="token function">getDeclaredFields</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                    <span class="token keyword">for</span> <span class="token punctuation">(</span>Field temp <span class="token operator">:</span> fields<span class="token punctuation">)</span> <span class="token punctuation">{</span>
                        <span class="token comment" spellcheck="true">//设置访问权限</span>
                        <span class="token comment" spellcheck="true">//temp.setAccessible(true);</span>
                        System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>temp<span class="token punctuation">.</span><span class="token function">getName</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                    <span class="token punctuation">}</span>
                    System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>"<span class="token operator">*</span><span class="token operator">*</span><span class="token operator">*</span><span class="token operator">*</span><span class="token operator">*</span><span class="token operator">*</span><span class="token operator">*</span><span class="token operator">*</span><span class="token operator">*</span><span class="token operator">*</span><span class="token operator">*</span><span class="token operator">*</span><span class="token operator">*</span><span class="token operator">*</span><span class="token operator">*</span><span class="token operator">*</span><span class="token operator">*</span><span class="token operator">*</span><span class="token operator">*</span><span class="token operator">*</span><span class="token operator">*</span><span class="token operator">*</span><span class="token operator">*</span><span class="token operator">*</span><span class="token operator">*</span><span class="token operator">*</span><span class="token operator">*</span><span class="token operator">*</span><span class="token operator">*</span><span class="token operator">*</span><span class="token operator">*</span><span class="token operator">*</span><span class="token operator">*</span><span class="token operator">*</span><span class="token operator">*</span><span class="token operator">*</span><span class="token operator">*</span><span class="token operator">*</span><span class="token operator">*</span><span class="token operator">*</span><span class="token operator">*</span><span class="token operator">*</span><span class="token operator">*</span><span class="token operator">*</span>
                            <span class="token comment" spellcheck="true">//查看所有方法</span>
                            Method<span class="token punctuation">[</span><span class="token punctuation">]</span>declaredMethods <span class="token operator">=</span> stuClass<span class="token punctuation">.</span><span class="token function">getDeclaredMethods</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                    <span class="token keyword">for</span> <span class="token punctuation">(</span>Method temp <span class="token operator">:</span> declaredMethods<span class="token punctuation">)</span> <span class="token punctuation">{</span>
                        <span class="token comment" spellcheck="true">//temp.getReturnType()返回方法的返回类型的反射形</span>
                        Class <span class="token class-name">returnType</span> <span class="token operator">=</span> temp<span class="token punctuation">.</span><span class="token function">getReturnType</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                        System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"返回类型:"</span> <span class="token operator">+</span> returnType<span class="token punctuation">.</span><span class="token function">getName</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                        System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"方法名:"</span> <span class="token operator">+</span> temp<span class="token punctuation">.</span><span class="token function">getName</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                        System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"参数列表:"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                        <span class="token comment" spellcheck="true">//返回参数列表</span>
                        Parameter<span class="token punctuation">[</span><span class="token punctuation">]</span> parameters <span class="token operator">=</span> temp<span class="token punctuation">.</span><span class="token function">getParameters</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                        <span class="token keyword">for</span> <span class="token punctuation">(</span>Parameter temp2 <span class="token operator">:</span> parameters<span class="token punctuation">)</span> <span class="token punctuation">{</span>
                            <span class="token comment" spellcheck="true">//参数类型</span>
                            System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"参数返回类型:"</span> <span class="token operator">+</span> temp2<span class="token punctuation">.</span><span class="token function">getType</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>getN
                                    <span class="token comment" spellcheck="true">//参数名称【JVM中是不需要知道参数名称的】</span>
                                    System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"参数名称:"</span> <span class="token operator">+</span> temp2<span class="token punctuation">.</span><span class="token function">getName</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                        <span class="token punctuation">}</span>
                        System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"*****************************"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                    <span class="token punctuation">}</span>
                    System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"*************构造方法****************"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                    <span class="token comment" spellcheck="true">//查看所有构造方法</span>
                    Constructor<span class="token punctuation">[</span><span class="token punctuation">]</span> declaredConstructors <span class="token operator">=</span> stuClass<span class="token punctuation">.</span>getDeclaredConstru
                    <span class="token keyword">for</span> <span class="token punctuation">(</span>Constructor temp <span class="token operator">:</span> declaredConstructors<span class="token punctuation">)</span> <span class="token punctuation">{</span>
                        <span class="token comment" spellcheck="true">//temp.getReturnType()返回方法的返回类型的反射形</span>
                        System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"方法名:"</span> <span class="token operator">+</span> temp<span class="token punctuation">.</span><span class="token function">getName</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                        System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"参数列表:"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                        <span class="token comment" spellcheck="true">//返回参数列表</span>
                        Parameter<span class="token punctuation">[</span><span class="token punctuation">]</span> parameters <span class="token operator">=</span> temp<span class="token punctuation">.</span><span class="token function">getParameters</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                        <span class="token keyword">for</span> <span class="token punctuation">(</span>Parameter temp2 <span class="token operator">:</span> parameters<span class="token punctuation">)</span> <span class="token punctuation">{</span>
                            <span class="token comment" spellcheck="true">//参数类型</span>
                            System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"参数返回类型:"</span> <span class="token operator">+</span> temp2<span class="token punctuation">.</span><span class="token function">getType</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>getN
                                    <span class="token comment" spellcheck="true">//参数名称【JVM中是不需要知道参数名称的】</span>
                                    System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"参数名称:"</span> <span class="token operator">+</span> temp2<span class="token punctuation">.</span><span class="token function">getName</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                        <span class="token punctuation">}</span>
                        System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"*****************************"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                    <span class="token punctuation">}</span>
                <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">ClassNotFoundException</span> e<span class="token punctuation">)</span> <span class="token punctuation">{</span>
                    e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                <span class="token punctuation">}</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span>
</code></pre>
<p> 构造类 </p>
<p>执⾏类属性 </p>
<p>执⾏类⽅法 </p>
<h1 id="七、注解"><a href="#七、注解" class="headerlink" title="七、注解"></a>七、注解</h1><h2 id="注解的作用"><a href="#注解的作用" class="headerlink" title="注解的作用"></a>注解的作用</h2><ul>
<li><p>注解是使⽤特定的符号和属性来完成特定的功能</p>
<ul>
<li>符号是@xxxxxx</li>
<li>属性是@xxxx(xxx=xxx,xxx=xxx) </li>
<li>注解使⽤⾮常简单，完成的特定功能都是封装了Java代码</li>
</ul>
</li>
<li><p>注解的⽣命周期 </p>
<ul>
<li><p>活在源代码（给⼈看，相当于注释）</p>
<p>RetentionPolicy.SOURCE</p>
</li>
<li><p>活在字节码（class字节码可以解析，相当于给字节码程序员当注释，字节码程序员可以利 ⽤该注解完成某些功能）</p>
<p>RetentionPolicy.CLASS</p>
</li>
<li><p>活在运⾏时（JVM）（JVM需要执⾏的，对功能有影响）</p>
<p>RetentionPolicy.RUNTIME</p>
</li>
</ul>
</li>
<li><p>注解可以⽤在哪⾥（Java源码） </p>
<p>注解可以写的地⽅由ElementType枚举类定义的地⽅</p>
</li>
</ul>
<h2 id="注解的使⽤"><a href="#注解的使⽤" class="headerlink" title="注解的使⽤"></a>注解的使⽤</h2><p>看注解的定义</p>
<p>看注解的功能</p>
<p>自定义操作 </p>
<p>注解的声明（⾃定义注解） </p>
<p>注解是⼀个接⼝，但是需要在接⼝前⾯使⽤@符号 </p>
<pre class=" language-java"><code class="language-java"><span class="token comment" spellcheck="true">//Override是注解名称</span>
<span class="token comment" spellcheck="true">//@interface是注解，所以区别于interface接口，但是注解就是接口</span>
<span class="token keyword">public</span> @<span class="token keyword">interface</span> <span class="token class-name">Override</span>
</code></pre>
<p>注解⽣命周期定义（⽤在注解上⾯的注解叫元注解） </p>
<blockquote>
<p>使用@Retention<br>具体的什么周期使用RetentionPolicy枚举</p>
</blockquote>
<p>注解能⽤在什么地⽅ </p>
<blockquote>
<p>使用@Target<br>具体的位置使用ElementType枚举</p>
</blockquote>
<p>注解能⽤的地⽅可以是多个（也就是说@Target可以声明出多个地⽅）</p>
<p>注解结构</p>
<p>属性（不带默认值的属性、带默认值的属性） </p>
<pre class=" language-java"><code class="language-java">        <span class="token comment" spellcheck="true">//生命周期</span>
        <span class="token annotation punctuation">@Retention</span><span class="token punctuation">(</span>RetentionPolicy<span class="token punctuation">.</span>SOURCE<span class="token punctuation">)</span>
        <span class="token comment" spellcheck="true">//能写在哪里（一般都会只定义写在一个地方，很少定义同时写到多个地方）</span>
        <span class="token annotation punctuation">@Target</span><span class="token punctuation">(</span><span class="token punctuation">{</span>ElementType<span class="token punctuation">.</span>TYPE<span class="token punctuation">,</span> ElementType<span class="token punctuation">.</span>FIELD<span class="token punctuation">}</span><span class="token punctuation">)</span>
        <span class="token keyword">public</span> @<span class="token keyword">interface</span> <span class="token class-name">MyAnnotaion</span> <span class="token punctuation">{</span>
            <span class="token comment" spellcheck="true">//注解属性</span>
            <span class="token comment" spellcheck="true">//不带默认值 数据类型 属性名()</span>
            <span class="token comment" spellcheck="true">//带默认值的 数据类型 属性名() default 值</span>
            String <span class="token function">name</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">default</span> <span class="token string">"hzj"</span><span class="token punctuation">;</span>

            <span class="token comment" spellcheck="true">//如果该属性没有默认值，则使用注解的时候必须填</span>
            <span class="token keyword">int</span> <span class="token function">age</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

            <span class="token keyword">int</span><span class="token punctuation">[</span><span class="token punctuation">]</span> <span class="token function">arrayAH</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token comment" spellcheck="true">//属性的数据类型，只能基本属性类型、字符串型、Class类型，其它的自定义类型等不可以</span>
            <span class="token comment" spellcheck="true">//容器可以是数组(但是只能是一维)，但是不能是集合</span>
            <span class="token comment" spellcheck="true">//ArrayList cl();</span>
        <span class="token punctuation">}</span>
</code></pre>
<h1 id="八、线程"><a href="#八、线程" class="headerlink" title="八、线程"></a>八、线程</h1><h2 id="1-进程（由线程组成）"><a href="#1-进程（由线程组成）" class="headerlink" title="1.进程（由线程组成）"></a>1.进程（由线程组成）</h2><ul>
<li><p>进程就是程序的⽣命周期，应⽤程序开始，进程开始，应⽤程序结束，进程结束</p>
</li>
<li><p>程序运⾏在操作系统上，进程是程序的单位</p>
</li>
<li><p>进程由线程组成，⼀个进程的启动，⾄少要有⼀个线程</p>
</li>
<li><p>Java程序中，主启动⽅法（⼊⼝⽅法）为main⽅法启动后⽴即产⽣了⼀个main线程（主线程）</p>
</li>
<li><p>进程拥有⽣命周期</p>
<ul>
<li>⼀个程序开始，进程开始</li>
<li>进程内的所有线程结束，则进程结束</li>
</ul>
</li>
</ul>
<h2 id="2-线程"><a href="#2-线程" class="headerlink" title="2.线程"></a>2.线程</h2><p>线程是进程的组成单位</p>
<p>线程拥有⽣命周期</p>
<p>线程由⽅法体语句组成 </p>
<p> 一个线程对应一个栈和一个程序计数器</p>
<ul>
<li>native⽅法（抽象⽅法，调⽤C语⾔的⽅法，Java可以和C语⾔通信）</li>
</ul>
<p>在⼀个类class⾥⾯会可以存在抽象⽅法，但是必须是native⽅法</p>
<p>native⽅法是指调⽤本地⽅法（该本地⽅法指的是C或C++语法的⽅法）</p>
<p>native⽅法意味着该⽅法并不是Java语法实现的，因此需要采⽤JNI技术来调⽤C或C++⽅法</p>
<p>⼀般来说涉及到了操作系统的底层或者直接要与硬件通信，通常会使⽤C语⾔处理，⽽Java采⽤navtic修饰符修饰⾃身的抽象⽅法，代表调⽤C语⾔的⽅法。  </p>
<p>Thread类</p>
<p>thread代表线程</p>
<p>获取当前线程</p>
<p>静态⽅法：currentThread() 获取线程名称</p>
<p>对象⽅法：getName() </p>
<p>线程的创建 </p>
<p>语法层⾯（都会使⽤实现runnable接⼝的⽅法来创建线程）</p>
<ul>
<li><p>继承Thread类（如果直接继承Thread类，那么就⽆法继承其他类）</p>
<ul>
<li>继承Thread类 </li>
<li>重写run⽅法 </li>
<li>使⽤Thread对象的start⽅法启动线程</li>
<li>实现Runnable接⼝（都会采⽤该⽅法）（因为没有⽤继承，因此还可以继承其它的类）</li>
</ul>
</li>
<li><p>实现Runnable接⼝</p>
<ul>
<li>重写run⽅法 </li>
<li>使⽤⼀个Thread对象与Runnable进⾏关联</li>
</ul>
</li>
</ul>
<pre class=" language-java"><code class="language-java">MyThreadB myThreadB<span class="token operator">=</span><span class="token keyword">new</span> <span class="token class-name">MyThreadB</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">//与Thread建立关联</span>
Thread thread<span class="token operator">=</span><span class="token keyword">new</span> <span class="token class-name">Thread</span><span class="token punctuation">(</span>myThreadB<span class="token punctuation">)</span><span class="token punctuation">;</span>
thread<span class="token punctuation">.</span><span class="token function">setName</span><span class="token punctuation">(</span><span class="token string">"MyThreadB线程"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
thread<span class="token punctuation">.</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>
<p>使⽤Thread对象的start⽅法启动线程 </p>
<p>内存层⾯</p>
<ul>
<li><p>创建⼀个线程就是创建了⼀个栈</p>
</li>
<li><p>异步：就在不同栈中执⾏⽅法，就是异步执⾏</p>
</li>
<li><p>堆是数据存储的，堆⾥⾯的对象是线程共享的</p>
</li>
<li><p>栈是线程私有</p>
</li>
</ul>
<p>线程处理共享数据的问题</p>
<ul>
<li><p>多个线程处理共享数据（⽐如对象，⽐如静态资源），会造成线程安全问题。</p>
</li>
<li><p>我们会采⽤同步策略解决问题</p>
<ul>
<li>加同步锁</li>
</ul>
</li>
</ul>
<p> 常⽤⽅法</p>
<ul>
<li><p>start()</p>
<p>启动当前线程；调用当前线程的run()</p>
</li>
<li><p>currentThread()</p>
<ul>
<li>获取当前线程</li>
</ul>
</li>
<li><p>sleep() </p>
<ul>
<li>线程阻塞，若⼲毫秒后⾃动唤醒</li>
<li>可以被中断（可以提前唤醒）</li>
<li>使⽤TimeUnit替代Thread.sleep，可以优雅的，仅此⽽已</li>
</ul>
</li>
</ul>
<pre class=" language-java"><code class="language-java"><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">MyApp6</span> <span class="token punctuation">{</span>
<span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span>String<span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token comment" spellcheck="true">//线程想休眠3小时24分17秒88毫秒</span>
<span class="token comment" spellcheck="true">//一般方案</span>
<span class="token keyword">try</span> <span class="token punctuation">{</span>
Thread<span class="token punctuation">.</span><span class="token function">sleep</span><span class="token punctuation">(</span>12257088L<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">InterruptedException</span> e<span class="token punctuation">)</span> <span class="token punctuation">{</span>
e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token comment" spellcheck="true">//线程想休眠3小时24分17秒88毫秒</span>
<span class="token comment" spellcheck="true">//优雅方案</span>
<span class="token keyword">try</span> <span class="token punctuation">{</span>
TimeUnit<span class="token punctuation">.</span>HOURS<span class="token punctuation">.</span><span class="token function">sleep</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
TimeUnit<span class="token punctuation">.</span>MINUTES<span class="token punctuation">.</span><span class="token function">sleep</span><span class="token punctuation">(</span><span class="token number">24</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
TimeUnit<span class="token punctuation">.</span>SECONDS<span class="token punctuation">.</span><span class="token function">sleep</span><span class="token punctuation">(</span><span class="token number">17</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
TimeUnit<span class="token punctuation">.</span>MILLISECONDS<span class="token punctuation">.</span><span class="token function">sleep</span><span class="token punctuation">(</span><span class="token number">88</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">InterruptedException</span> e<span class="token punctuation">)</span> <span class="token punctuation">{</span>
e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<ul>
<li><p>setName/getName</p>
<p>设置线程名称，底层是⼀个char数组（猜想：因为c没有字符串，c的字符串是char数 组）</p>
</li>
<li><p>isAlive()</p>
<p>查询线程是否启动并没终⽌状态</p>
</li>
<li><p>getId()</p>
<p>线程唯⼀ID，底层是⼀个简单的⾃增的整数</p>
</li>
<li><p>yield()</p>
<p>⽅法的作⽤是放弃当前的CPU资源（不确定）</p>
</li>
<li><p>setPriority()</p>
<p>设置线程优先级（不确定）</p>
</li>
<li><p>setDaemon() </p>
<p>设置后台线程</p>
<p>后台线程不可单独运⾏（如果全是后台线程，则⽴即⾃⼰消亡）</p>
</li>
</ul>
<pre class=" language-java"><code class="language-java"><span class="token keyword">package</span> com<span class="token punctuation">.</span>bjpowernode<span class="token punctuation">.</span>thread<span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">//测试后台线程，进程中只有后台线程，那么后台线程立即死亡</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">MyT4</span> <span class="token keyword">implements</span> <span class="token class-name">Runnable</span><span class="token punctuation">{</span>
<span class="token annotation punctuation">@Override</span>
<span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">run</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token keyword">while</span> <span class="token punctuation">(</span><span class="token boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"okokokko"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">try</span> <span class="token punctuation">{</span>
Thread<span class="token punctuation">.</span><span class="token function">sleep</span><span class="token punctuation">(</span><span class="token number">500</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">InterruptedException</span> e<span class="token punctuation">)</span> <span class="token punctuation">{</span>
e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
<span class="token keyword">package</span> com<span class="token punctuation">.</span>bjpowernode<span class="token punctuation">.</span>app<span class="token punctuation">;</span>
<span class="token keyword">import</span> com<span class="token punctuation">.</span>bjpowernode<span class="token punctuation">.</span>thread<span class="token punctuation">.</span>MyT4<span class="token punctuation">;</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">MyApp4</span> <span class="token punctuation">{</span>
<span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span>String<span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span>
MyT4 myT4<span class="token operator">=</span><span class="token keyword">new</span> <span class="token class-name">MyT4</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
Thread thread<span class="token operator">=</span><span class="token keyword">new</span> <span class="token class-name">Thread</span><span class="token punctuation">(</span>myT4<span class="token punctuation">,</span><span class="token string">"myT4"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">//这里设置一个后台线程，并且该线程是死循环</span>
thread<span class="token punctuation">.</span><span class="token function">setDaemon</span><span class="token punctuation">(</span><span class="token boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">//开启线程，然后什么都不会打印，线程就死掉了</span>
thread<span class="token punctuation">.</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<h2 id="3-中断"><a href="#3-中断" class="headerlink" title="3.中断"></a>3.中断</h2><ul>
<li><p>中断可以中断的⽅法有</p>
<p>sleep()</p>
<p> join()：在线程a中调用线程b的join()，此时线程a就进入阻塞状态，直到线程n完全执行完以后，线程a				才结束阻塞状态</p>
</li>
<li><p>中断有三个⽅法</p>
</li>
</ul>
<p>线程对象中断操作</p>
<ul>
<li><p>interrupt()【如果没有遇到需要唤醒的线程，则保留标记位为true，唤醒后则标记位为 false】</p>
</li>
<li><p>该⽅法可以让Thread.sleep唤醒（也就是中断Thread的sleep让其提前唤醒</p>
<ul>
<li>被中断的sleep会出现在catch块中（也就是Java使⽤了try catch捕获中断</li>
</ul>
</li>
</ul>
<p>线程类静态判断中断（使⽤后会修改标记位，并修改为false)</p>
<ul>
<li><p>interrupted()【该⽅法属于静态⽅法Thread.interrupted()】</p>
<ul>
<li><p>判断中断⽅法是否使⽤过，采⽤的标记位⽅法</p>
<p>如果interrupt()⽅法没有中断（也就是没有踢掉sleep）则interrupted返回true</p>
<p>如果interrupt()⽅法有中断（也就是有踢掉sleep）则interrupted返回false</p>
</li>
<li><p>若没有interrupt()⽅法，则interrupted的标记位false</p>
</li>
</ul>
</li>
<li><p>interrupted()⽅法执⾏过后会修改标记位为false</p>
</li>
</ul>
<p>线程对象判断中断（该⽅法属于对象⽅法Thread.currentThread().isInterrupted()）</p>
<ul>
<li><p>判断中断⽅法是否使⽤过，采⽤的标记位⽅法</p>
<p>如果interrupt()⽅法没有中断（也就是没有踢掉sleep）则interrupted返回true</p>
<p>如果interrupt()⽅法有中断（也就是有踢掉sleep）则interrupted返回false</p>
</li>
<li><p>若没有interrupt()⽅法，则interrupted的标记位false</p>
<p>isInterrupted()⽅法执⾏过后不会修改标记位</p>
</li>
</ul>
<p> interrupt()中断sleep  </p>
<pre class=" language-java"><code class="language-java"><span class="token keyword">package</span> com<span class="token punctuation">.</span>bjpowernode<span class="token punctuation">.</span>thread<span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">//interrupt()中断sleep</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">MyT2</span> <span class="token keyword">implements</span> <span class="token class-name">Runnable</span><span class="token punctuation">{</span>
<span class="token annotation punctuation">@Override</span>
<span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">run</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token comment" spellcheck="true">//执行中断开始设计标记位为true,</span>
<span class="token comment" spellcheck="true">//碰到sleep将会立即中断sleep线程将其唤醒</span>
Thread<span class="token punctuation">.</span><span class="token function">currentThread</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">interrupt</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">try</span> <span class="token punctuation">{</span>
<span class="token comment" spellcheck="true">//开始sleep</span>
<span class="token comment" spellcheck="true">//interrupt()可以将其唤醒</span>
Thread<span class="token punctuation">.</span><span class="token function">sleep</span><span class="token punctuation">(</span><span class="token number">500</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">InterruptedException</span> e<span class="token punctuation">)</span> <span class="token punctuation">{</span>
e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"被踢了"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token comment" spellcheck="true">//判断中断标记位【这里使用的是Thread.interrupted()】</span>
<span class="token comment" spellcheck="true">//判断后会修改标记位为false</span>
System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>Thread<span class="token punctuation">.</span><span class="token function">interrupted</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
<span class="token keyword">package</span> com<span class="token punctuation">.</span>bjpowernode<span class="token punctuation">.</span>app<span class="token punctuation">;</span>
<span class="token keyword">import</span> com<span class="token punctuation">.</span>bjpowernode<span class="token punctuation">.</span>thread<span class="token punctuation">.</span>MyT2<span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">//interrupt()中断sleep</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">MyApp2</span> <span class="token punctuation">{</span>
<span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span>String<span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span>
MyT2 myT2<span class="token operator">=</span><span class="token keyword">new</span> <span class="token class-name">MyT2</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
Thread thread<span class="token operator">=</span><span class="token keyword">new</span> <span class="token class-name">Thread</span><span class="token punctuation">(</span>myT2<span class="token punctuation">,</span><span class="token string">"myT2"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
thread<span class="token punctuation">.</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<p> main线程中中断thread线程 </p>
<pre class=" language-java"><code class="language-java"><span class="token keyword">package</span> com<span class="token punctuation">.</span>bjpowernode<span class="token punctuation">.</span>thread<span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">//在外界使用interrupt()方法中断本线程</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">MyT1</span> <span class="token keyword">implements</span> <span class="token class-name">Runnable</span> <span class="token punctuation">{</span>
<span class="token annotation punctuation">@Override</span>
<span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">run</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">;</span> i <span class="token operator">&lt;=</span> <span class="token number">10</span><span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">try</span> <span class="token punctuation">{</span>
Thread<span class="token punctuation">.</span><span class="token function">sleep</span><span class="token punctuation">(</span><span class="token number">60000</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">InterruptedException</span> e<span class="token punctuation">)</span> <span class="token punctuation">{</span>
e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"MyT1的被踢醒"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
<span class="token keyword">package</span> com<span class="token punctuation">.</span>bjpowernode<span class="token punctuation">.</span>app<span class="token punctuation">;</span>
<span class="token keyword">import</span> com<span class="token punctuation">.</span>bjpowernode<span class="token punctuation">.</span>thread<span class="token punctuation">.</span>MyT1<span class="token punctuation">;</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">MyApp</span> <span class="token punctuation">{</span>
<span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span>String<span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span>
MyT1 myT1<span class="token operator">=</span><span class="token keyword">new</span> <span class="token class-name">MyT1</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
Thread thread<span class="token operator">=</span><span class="token keyword">new</span> <span class="token class-name">Thread</span><span class="token punctuation">(</span>myT1<span class="token punctuation">,</span><span class="token string">"线程1"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
thread<span class="token punctuation">.</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">try</span> <span class="token punctuation">{</span>
Thread<span class="token punctuation">.</span><span class="token function">sleep</span><span class="token punctuation">(</span><span class="token number">5000</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">InterruptedException</span> e<span class="token punctuation">)</span> <span class="token punctuation">{</span>
e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token comment" spellcheck="true">//使用main线程中断thread线程【也就是不在thread自己里面中断自己】</span>
thread<span class="token punctuation">.</span><span class="token function">interrupt</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<p> Thread.currentThread().isInterrupted()调⽤后不会修改标记位 </p>
<pre class=" language-java"><code class="language-java"><span class="token keyword">package</span> com<span class="token punctuation">.</span>bjpowernode<span class="token punctuation">.</span>thread<span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">//测试Thread.currentThread().isInterrupted()判断标记位后，不会改变标记位</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">MyT5</span> <span class="token keyword">implements</span> <span class="token class-name">Runnable</span><span class="token punctuation">{</span>
<span class="token annotation punctuation">@Override</span>
<span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">run</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token comment" spellcheck="true">//调用中断</span>
Thread<span class="token punctuation">.</span><span class="token function">currentThread</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">interrupt</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">//打印中断标记位，这里为true,并且不会改变标记位</span>
System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>Thread<span class="token punctuation">.</span><span class="token function">currentThread</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">isInterrupted</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">//打印中断标记位，这里为true,并且不会改变标记位</span>
System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>Thread<span class="token punctuation">.</span><span class="token function">currentThread</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">isInterrupted</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
<span class="token keyword">package</span> com<span class="token punctuation">.</span>bjpowernode<span class="token punctuation">.</span>app<span class="token punctuation">;</span>
<span class="token keyword">import</span> com<span class="token punctuation">.</span>bjpowernode<span class="token punctuation">.</span>thread<span class="token punctuation">.</span>MyT5<span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">//测试Thread.currentThread().isInterrupted()判断标记位后，不会改变标记位</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">MyApp5</span> <span class="token punctuation">{</span>
<span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span>String<span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span>
MyT5 myT5 <span class="token operator">=</span><span class="token keyword">new</span> <span class="token class-name">MyT5</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
Thread thread1<span class="token operator">=</span><span class="token keyword">new</span> <span class="token class-name">Thread</span><span class="token punctuation">(</span>myT5<span class="token punctuation">,</span><span class="token string">"xxx"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
thread1<span class="token punctuation">.</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<p> Thread.interrupted()调⽤后会修改标记位为false </p>
<pre class=" language-java"><code class="language-java"><span class="token keyword">package</span> com<span class="token punctuation">.</span>bjpowernode<span class="token punctuation">.</span>thread<span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">//测试Thread.interrupted()判断标记位后，会改变标记位为false</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">MyT3</span> <span class="token keyword">implements</span> <span class="token class-name">Runnable</span> <span class="token punctuation">{</span>
<span class="token annotation punctuation">@Override</span>
<span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">run</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token comment" spellcheck="true">//调用中断</span>
Thread<span class="token punctuation">.</span><span class="token function">currentThread</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">interrupt</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">//打印中断标记位，这里为true,并且会改变标记位为false</span>
System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>Thread<span class="token punctuation">.</span><span class="token function">interrupted</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">//打印中断标记位，这里为false,因为被上面的Thread.interrupted()修改了</span>
System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>Thread<span class="token punctuation">.</span><span class="token function">interrupted</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
<span class="token keyword">package</span> com<span class="token punctuation">.</span>bjpowernode<span class="token punctuation">.</span>app<span class="token punctuation">;</span>
<span class="token keyword">import</span> com<span class="token punctuation">.</span>bjpowernode<span class="token punctuation">.</span>thread<span class="token punctuation">.</span>MyT3<span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">//测试Thread.interrupted()判断标记位后，会改变标记位为false</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">MyApp3</span> <span class="token punctuation">{</span>
<span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span>String<span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span>
MyT3 myT3 <span class="token operator">=</span><span class="token keyword">new</span> <span class="token class-name">MyT3</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
Thread thread1<span class="token operator">=</span><span class="token keyword">new</span> <span class="token class-name">Thread</span><span class="token punctuation">(</span>myT3<span class="token punctuation">,</span><span class="token string">"xxx"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
thread1<span class="token punctuation">.</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<p> join</p>
<ul>
<li><p>线程A在线程B中执⾏了A.join()，则B进⼊等待，直到线程A结束⽣命周期，或者到达给定的 时间，那么B线程继续执⾏</p>
</li>
<li><p>join⽅法会使当前线程永远地等待下去，直到期间被另外的线程中断，或者join的线程执⾏ 结束</p>
</li>
</ul>
<p> 状态 </p>
<ul>
<li><p>线程的状态由Thread.State枚举常量标识： </p>
</li>
<li><p>NEW——线程还没有开始执⾏。  </p>
</li>
<li><p>RUNNABLE——线程正在JVM中执⾏  </p>
</li>
<li><p>BLOCKED——线程被阻塞并等待⼀个监听锁  </p>
</li>
<li><p>WAITING——线程⽆限期地等待另外⼀条线程执⾏特定的操作 </p>
</li>
<li><p>TIMED_WAITING——线程在特定的时间内等待另外⼀条线程执⾏某种操作。 </p>
</li>
<li><p>TERMINATED——线程⼰经退出 </p>
</li>
<li><p>判断线程状态的⼀些⽅法</p>
<ul>
<li>isAlive()检查线程是否已经启动且还没有结束状态</li>
<li>getState()⽅法直接获取线程对象的状态</li>
</ul>
</li>
</ul>
<pre class=" language-java"><code class="language-java"><span class="token keyword">package</span> com<span class="token punctuation">.</span>bjpowernode<span class="token punctuation">.</span>thread<span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">//线程状态查看</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">MyT7</span> <span class="token keyword">implements</span> <span class="token class-name">Runnable</span> <span class="token punctuation">{</span>
<span class="token annotation punctuation">@Override</span>
<span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">run</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token comment" spellcheck="true">//RUNNABLE状态</span>
<span class="token comment" spellcheck="true">//thread线程开始执行状态</span>
System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"RUNNABLE-1状态测试:"</span><span class="token operator">+</span>Thread<span class="token punctuation">.</span><span class="token function">currentThread</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>getSt
System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"ok"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">try</span> <span class="token punctuation">{</span>
<span class="token comment" spellcheck="true">//thread线程开始休眠</span>
Thread<span class="token punctuation">.</span><span class="token function">sleep</span><span class="token punctuation">(</span><span class="token number">1000</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">InterruptedException</span> e<span class="token punctuation">)</span> <span class="token punctuation">{</span>
e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">//RUNNABLE状态</span>
<span class="token comment" spellcheck="true">//thread线程休眠被中断，开始执行</span>
System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"RUNNABLE-3状态测试（唤醒）:"</span><span class="token operator">+</span>Thread<span class="token punctuation">.</span>currentThr
<span class="token punctuation">}</span>
<span class="token comment" spellcheck="true">//RUNNABLE状态</span>
<span class="token comment" spellcheck="true">//thread线程继续执行</span>
System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"RUNNABLE-2状态测试:"</span><span class="token operator">+</span>Thread<span class="token punctuation">.</span><span class="token function">currentThread</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>getSt
<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
<span class="token keyword">package</span> com<span class="token punctuation">.</span>bjpowernode<span class="token punctuation">.</span>app<span class="token punctuation">;</span>
<span class="token keyword">import</span> com<span class="token punctuation">.</span>bjpowernode<span class="token punctuation">.</span>thread<span class="token punctuation">.</span>MyT7<span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">//获取线程的当前状态</span>
<span class="token comment" spellcheck="true">//尝试自己写出打印语句，并且正好是thread对象所处的某种状态</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">MyApp7</span> <span class="token punctuation">{</span>
<span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span>String<span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span>
MyT7 myT7<span class="token operator">=</span><span class="token keyword">new</span> <span class="token class-name">MyT7</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
Thread thread<span class="token operator">=</span><span class="token keyword">new</span> <span class="token class-name">Thread</span><span class="token punctuation">(</span>myT7<span class="token punctuation">,</span><span class="token string">"myT7"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">//NEW状态</span>
<span class="token comment" spellcheck="true">//thread线程刚刚新建</span>
System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"NEW状态测试:"</span><span class="token operator">+</span>thread<span class="token punctuation">.</span><span class="token function">getState</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">//thread线程刚刚开始</span>
thread<span class="token punctuation">.</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">//这里main线程休眠500，目的是为了让thread线程抢到CPU进入休眠</span>
<span class="token comment" spellcheck="true">//为下面打印thread线程休眠状态提供支持</span>
<span class="token keyword">try</span> <span class="token punctuation">{</span>
Thread<span class="token punctuation">.</span><span class="token function">sleep</span><span class="token punctuation">(</span><span class="token number">500</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">InterruptedException</span> e<span class="token punctuation">)</span> <span class="token punctuation">{</span>
e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token comment" spellcheck="true">//TIMED_WAITING状态</span>
<span class="token comment" spellcheck="true">//thread线程开始休眠</span>
System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"TIMED_WAITING状态测试:"</span><span class="token operator">+</span>thread<span class="token punctuation">.</span><span class="token function">getState</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">//唤醒thread</span>
<span class="token comment" spellcheck="true">//thread线程休眠被唤醒</span>
thread<span class="token punctuation">.</span><span class="token function">interrupt</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">//这里main线程休眠3000，目的是为了让thread线程抢到CPU,并执行完毕</span>
<span class="token comment" spellcheck="true">//为下面打印thread线程结束状态提供支持</span>
<span class="token keyword">try</span> <span class="token punctuation">{</span>
Thread<span class="token punctuation">.</span><span class="token function">sleep</span><span class="token punctuation">(</span><span class="token number">3000</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">InterruptedException</span> e<span class="token punctuation">)</span> <span class="token punctuation">{</span>
e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token comment" spellcheck="true">//TERMINATED状态</span>
<span class="token comment" spellcheck="true">//thread已经执行结束（这里利用时间差预判thread已经执行结束）</span>
System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"TERMINATED状态测试:"</span><span class="token operator">+</span>thread<span class="token punctuation">.</span><span class="token function">getState</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<p>⽣命周期</p>
<ul>
<li><p>线程从⽣到死的状态变化</p>
</li>
<li><p>执⾏各种线程⽅法后，线程的状态变化</p>
</li>
</ul>
<p> 线程关闭【thread.stop();已经被废除】</p>
<ul>
<li><p>正常关闭，线程执⾏完毕即关闭</p>
</li>
<li><p>捕获中断信号关闭线程【这⾥不推荐使⽤该⽅法来设计线程关闭】</p>
</li>
<li><p>使⽤标记控制【最好使⽤ volatile修饰 】</p>
</li>
</ul>

                
            </div>
            <hr/>

            

    <div class="reprint" id="reprint-statement">
        
            <div class="reprint__author">
                <span class="reprint-meta" style="font-weight: bold;">
                    <i class="fas fa-user">
                        文章作者:
                    </i>
                </span>
                <span class="reprint-info">
                    <a href="/about" rel="external nofollow noreferrer">YangChao</a>
                </span>
            </div>
            <div class="reprint__type">
                <span class="reprint-meta" style="font-weight: bold;">
                    <i class="fas fa-link">
                        文章链接:
                    </i>
                </span>
                <span class="reprint-info">
                    <a href="https://monkey-chao.gitee.io/2020/05/07/java-ji-chu-pian-er/">https://monkey-chao.gitee.io/2020/05/07/java-ji-chu-pian-er/</a>
                </span>
            </div>
            <div class="reprint__notice">
                <span class="reprint-meta" style="font-weight: bold;">
                    <i class="fas fa-copyright">
                        版权声明:
                    </i>
                </span>
                <span class="reprint-info">
                    本博客所有文章除特別声明外，均采用
                    <a href="https://creativecommons.org/licenses/by/4.0/deed.zh" rel="external nofollow noreferrer" target="_blank">CC BY 4.0</a>
                    许可协议。转载请注明来源
                    <a href="/about" target="_blank">YangChao</a>
                    !
                </span>
            </div>
        
    </div>

    <script async defer>
      document.addEventListener("copy", function (e) {
        let toastHTML = '<span>复制成功，请遵循本文的转载规则</span><button class="btn-flat toast-action" onclick="navToReprintStatement()" style="font-size: smaller">查看</a>';
        M.toast({html: toastHTML})
      });

      function navToReprintStatement() {
        $("html, body").animate({scrollTop: $("#reprint-statement").offset().top - 80}, 800);
      }
    </script>



            <div class="tag_share" style="display: block;">
                <div class="post-meta__tag-list" style="display: inline-block;">
                    
                        <div class="article-tag">
                            
                                <a href="/tags/Java/">
                                    <span class="chip bg-color">Java</span>
                                </a>
                            
                                <a href="/tags/%E6%95%99%E7%A8%8B/">
                                    <span class="chip bg-color">教程</span>
                                </a>
                            
                                <a href="/tags/%E5%9F%BA%E7%A1%80/">
                                    <span class="chip bg-color">基础</span>
                                </a>
                            
                        </div>
                    
                </div>
                <div class="post_share" style="zoom: 80%; width: fit-content; display: inline-block; float: right; margin: -0.15rem 0;">
                    <link rel="stylesheet" type="text/css" href="/libs/share/css/share.min.css">
<div id="article-share">

    
    <div class="social-share" data-sites="twitter,facebook,google,qq,qzone,wechat,weibo,douban,linkedin" data-wechat-qrcode-helper="<p>微信扫一扫即可分享！</p>"></div>
    <script src="/libs/share/js/social-share.min.js"></script>
    

    

</div>

                </div>
            </div>
            
                <style>
    #reward {
        margin: 40px 0;
        text-align: center;
    }

    #reward .reward-link {
        font-size: 1.4rem;
        line-height: 38px;
    }

    #reward .btn-floating:hover {
        box-shadow: 0 6px 12px rgba(0, 0, 0, 0.2), 0 5px 15px rgba(0, 0, 0, 0.2);
    }

    #rewardModal {
        width: 320px;
        height: 350px;
    }

    #rewardModal .reward-title {
        margin: 15px auto;
        padding-bottom: 5px;
    }

    #rewardModal .modal-content {
        padding: 10px;
    }

    #rewardModal .close {
        position: absolute;
        right: 15px;
        top: 15px;
        color: rgba(0, 0, 0, 0.5);
        font-size: 1.3rem;
        line-height: 20px;
        cursor: pointer;
    }

    #rewardModal .close:hover {
        color: #ef5350;
        transform: scale(1.3);
        -moz-transform:scale(1.3);
        -webkit-transform:scale(1.3);
        -o-transform:scale(1.3);
    }

    #rewardModal .reward-tabs {
        margin: 0 auto;
        width: 210px;
    }

    .reward-tabs .tabs {
        height: 38px;
        margin: 10px auto;
        padding-left: 0;
    }

    .reward-content ul {
        padding-left: 0 !important;
    }

    .reward-tabs .tabs .tab {
        height: 38px;
        line-height: 38px;
    }

    .reward-tabs .tab a {
        color: #fff;
        background-color: #ccc;
    }

    .reward-tabs .tab a:hover {
        background-color: #ccc;
        color: #fff;
    }

    .reward-tabs .wechat-tab .active {
        color: #fff !important;
        background-color: #22AB38 !important;
    }

    .reward-tabs .alipay-tab .active {
        color: #fff !important;
        background-color: #019FE8 !important;
    }

    .reward-tabs .reward-img {
        width: 210px;
        height: 210px;
    }
</style>

<div id="reward">
    <a href="#rewardModal" class="reward-link modal-trigger btn-floating btn-medium waves-effect waves-light red">赏</a>

    <!-- Modal Structure -->
    <div id="rewardModal" class="modal">
        <div class="modal-content">
            <a class="close modal-close"><i class="fas fa-times"></i></a>
            <h4 class="reward-title">你的赏识是我前进的动力</h4>
            <div class="reward-content">
                <div class="reward-tabs">
                    <ul class="tabs row">
                        <li class="tab col s6 alipay-tab waves-effect waves-light"><a href="#alipay">支付宝</a></li>
                        <li class="tab col s6 wechat-tab waves-effect waves-light"><a href="#wechat">微 信</a></li>
                    </ul>
                    <div id="alipay">
                        <img src="/medias/reward/alipay.jpg" class="reward-img" alt="支付宝打赏二维码">
                    </div>
                    <div id="wechat">
                        <img src="/medias/reward/wechat.png" class="reward-img" alt="微信打赏二维码">
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
    $(function () {
        $('.tabs').tabs();
    });
</script>

            
        </div>
    </div>

    

    

    

    
    <div class="livere-card card" data-aos="fade-up">
    <!-- 来必力City版安装代码 -->
    <div id="lv-container" class="card-content" data-id="city" data-uid="">
        <script type="text/javascript">
            (function (d, s) {
                let j, e = d.getElementsByTagName(s)[0];
                if (typeof LivereTower === 'function') {
                    return;
                }

                j = d.createElement(s);
                j.src = 'https://cdn-city.livere.com/js/embed.dist.js';
                j.async = true;

                e.parentNode.insertBefore(j, e);
            })(document, 'script');
        </script>
        <noscript>为正常使用来必力评论功能请激活JavaScript。</noscript>
    </div>
    <!-- City版安装代码已完成 -->
</div>
    

    
        <style>
    .valine-card {
        margin: 1.5rem auto;
    }

    .valine-card .card-content {
        padding: 20px 20px 5px 20px;
    }

    #vcomments textarea {
        box-sizing: border-box;
        background: url("/medias/comment_bg.png") 100% 100% no-repeat;
    }

    #vcomments p {
        margin: 2px 2px 10px;
        font-size: 1.05rem;
        line-height: 1.78rem;
    }

    #vcomments blockquote p {
        text-indent: 0.2rem;
    }

    #vcomments a {
        padding: 0 2px;
        color: #4cbf30;
        font-weight: 500;
        text-decoration: none;
    }

    #vcomments img {
        max-width: 100%;
        height: auto;
        cursor: pointer;
    }

    #vcomments ol li {
        list-style-type: decimal;
    }

    #vcomments ol,
    ul {
        display: block;
        padding-left: 2em;
        word-spacing: 0.05rem;
    }

    #vcomments ul li,
    ol li {
        display: list-item;
        line-height: 1.8rem;
        font-size: 1rem;
    }

    #vcomments ul li {
        list-style-type: disc;
    }

    #vcomments ul ul li {
        list-style-type: circle;
    }

    #vcomments table, th, td {
        padding: 12px 13px;
        border: 1px solid #dfe2e5;
    }

    #vcomments table, th, td {
        border: 0;
    }

    table tr:nth-child(2n), thead {
        background-color: #fafafa;
    }

    #vcomments table th {
        background-color: #f2f2f2;
        min-width: 80px;
    }

    #vcomments table td {
        min-width: 80px;
    }

    #vcomments h1 {
        font-size: 1.85rem;
        font-weight: bold;
        line-height: 2.2rem;
    }

    #vcomments h2 {
        font-size: 1.65rem;
        font-weight: bold;
        line-height: 1.9rem;
    }

    #vcomments h3 {
        font-size: 1.45rem;
        font-weight: bold;
        line-height: 1.7rem;
    }

    #vcomments h4 {
        font-size: 1.25rem;
        font-weight: bold;
        line-height: 1.5rem;
    }

    #vcomments h5 {
        font-size: 1.1rem;
        font-weight: bold;
        line-height: 1.4rem;
    }

    #vcomments h6 {
        font-size: 1rem;
        line-height: 1.3rem;
    }

    #vcomments p {
        font-size: 1rem;
        line-height: 1.5rem;
    }

    #vcomments hr {
        margin: 12px 0;
        border: 0;
        border-top: 1px solid #ccc;
    }

    #vcomments blockquote {
        margin: 15px 0;
        border-left: 5px solid #42b983;
        padding: 1rem 0.8rem 0.3rem 0.8rem;
        color: #666;
        background-color: rgba(66, 185, 131, .1);
    }

    #vcomments pre {
        font-family: monospace, monospace;
        padding: 1.2em;
        margin: .5em 0;
        background: #272822;
        overflow: auto;
        border-radius: 0.3em;
        tab-size: 4;
    }

    #vcomments code {
        font-family: monospace, monospace;
        padding: 1px 3px;
        font-size: 0.92rem;
        color: #e96900;
        background-color: #f8f8f8;
        border-radius: 2px;
    }

    #vcomments pre code {
        font-family: monospace, monospace;
        padding: 0;
        color: #e8eaf6;
        background-color: #272822;
    }

    #vcomments pre[class*="language-"] {
        padding: 1.2em;
        margin: .5em 0;
    }

    #vcomments code[class*="language-"],
    pre[class*="language-"] {
        color: #e8eaf6;
    }

    #vcomments [type="checkbox"]:not(:checked), [type="checkbox"]:checked {
        position: inherit;
        margin-left: -1.3rem;
        margin-right: 0.4rem;
        margin-top: -1px;
        vertical-align: middle;
        left: unset;
        visibility: visible;
    }

    #vcomments b,
    strong {
        font-weight: bold;
    }

    #vcomments dfn {
        font-style: italic;
    }

    #vcomments small {
        font-size: 85%;
    }

    #vcomments cite {
        font-style: normal;
    }

    #vcomments mark {
        background-color: #fcf8e3;
        padding: .2em;
    }

    #vcomments table, th, td {
        padding: 12px 13px;
        border: 1px solid #dfe2e5;
    }

    table tr:nth-child(2n), thead {
        background-color: #fafafa;
    }

    #vcomments table th {
        background-color: #f2f2f2;
        min-width: 80px;
    }

    #vcomments table td {
        min-width: 80px;
    }

    #vcomments [type="checkbox"]:not(:checked), [type="checkbox"]:checked {
        position: inherit;
        margin-left: -1.3rem;
        margin-right: 0.4rem;
        margin-top: -1px;
        vertical-align: middle;
        left: unset;
        visibility: visible;
    }
</style>

<div class="card valine-card" data-aos="fade-up">
    <div class="comment_headling" style="font-size: 20px; font-weight: 700; position: relative; padding-left: 20px; top: 15px; padding-bottom: 5px;">
        <i class="fas fa-comments fa-fw" aria-hidden="true"></i>
        <span>评论</span>
    </div>
    <div id="vcomments" class="card-content" style="display: grid">
    </div>
</div>

<script src="/libs/valine/av-min.js"></script>
<script src="/libs/valine/Valine.min.js"></script>
<script>
    new Valine({
        el: '#vcomments',
        appId: '1543373971',
        appKey: '111111',
        notify: 'false' === 'true',
        verify: 'false' === 'true',
        visitor: 'true' === 'true',
        avatar: 'mm',
        pageSize: '10',
        lang: 'zh-cn',
        placeholder: 'just go go'
    });
</script>

<!--酷Q推送-->


    

    
        <style>
    .mvaline-card {
        margin: 1.5rem auto;
    }

    .mvaline-card .card-content {
        padding: 20px 20px 5px 20px;
    }
</style>

<div class="card mvaline-card" data-aos="fade-up">
    <div class="comment_headling" style="font-size: 20px; font-weight: 700; position: relative; padding-left: 20px; top: 15px; padding-bottom: 5px;">
        <i class="fas fa-comments fa-fw" aria-hidden="true"></i>
        <span>评论</span>
    </div>
    <div id="mvcomments" class="card-content" style="display: grid">
    </div>
</div>

<script src="/libs/minivaline/MiniValine.js"></script>
<script>
    new MiniValine(Object.assign({"enable":true,"serverURL":"https://minivaline.your-domain.top"}, {
	  el: '#mvcomments',
    }));
</script>

    

    

    

    

    

<article id="prenext-posts" class="prev-next articles">
    <div class="row article-row">
        
        <div class="article col s12 m6" data-aos="fade-up">
            <div class="article-badge left-badge text-color">
                <i class="fas fa-chevron-left"></i>&nbsp;上一篇</div>
            <div class="card">
                <a href="/2020/06/13/tu-pian-yan-zheng-ma-gong-neng-de-shi-xian-ji-yu-springboot/">
                    <div class="card-image">
                        
                        
                        <img src="/medias/featureimages/19.jpg" class="responsive-img" alt="图片验证码功能的实现（基于SpringBoot）">
                        
                        <span class="card-title">图片验证码功能的实现（基于SpringBoot）</span>
                    </div>
                </a>
                <div class="card-content article-content">
                    <div class="summary block-with-text">
                        
                            图片验证码功能的实现（基于SpringBoot）
                        
                    </div>
                    <div class="publish-info">
                        <span class="publish-date">
                            <i class="far fa-clock fa-fw icon-date"></i>2020-06-13
                        </span>
                        <span class="publish-author">
                            
                            <i class="fas fa-bookmark fa-fw icon-category"></i>
                            
                            <a href="/categories/Java%E5%AE%9E%E7%94%A8%E6%95%99%E7%A8%8B/" class="post-category">
                                    Java实用教程
                                </a>
                            
                            
                        </span>
                    </div>
                </div>
                
                <div class="card-action article-tags">
                    
                    <a href="/tags/Java/">
                        <span class="chip bg-color">Java</span>
                    </a>
                    
                    <a href="/tags/%E6%95%99%E7%A8%8B/">
                        <span class="chip bg-color">教程</span>
                    </a>
                    
                </div>
                
            </div>
        </div>
        
        
        <div class="article col s12 m6" data-aos="fade-up">
            <div class="article-badge right-badge text-color">
                本篇&nbsp;<i class="far fa-dot-circle"></i>
            </div>
            <div class="card">
                <a href="/2020/05/07/java-ji-chu-pian-er/">
                    <div class="card-image">
                        
                        
                        <img src="/medias/featureimages/2.jpg" class="responsive-img" alt="Java基础篇（二）">
                        
                        <span class="card-title">Java基础篇（二）</span>
                    </div>
                </a>
                <div class="card-content article-content">
                    <div class="summary block-with-text">
                        
                            Java基础入门篇
                        
                    </div>
                    <div class="publish-info">
                            <span class="publish-date">
                                <i class="far fa-clock fa-fw icon-date"></i>2020-05-07
                            </span>
                        <span class="publish-author">
                            
                            <i class="fas fa-bookmark fa-fw icon-category"></i>
                            
                            <a href="/categories/Java%E5%9F%BA%E7%A1%80%E6%95%99%E7%A8%8B/" class="post-category">
                                    Java基础教程
                                </a>
                            
                            
                        </span>
                    </div>
                </div>

                
                <div class="card-action article-tags">
                    
                    <a href="/tags/Java/">
                        <span class="chip bg-color">Java</span>
                    </a>
                    
                    <a href="/tags/%E6%95%99%E7%A8%8B/">
                        <span class="chip bg-color">教程</span>
                    </a>
                    
                    <a href="/tags/%E5%9F%BA%E7%A1%80/">
                        <span class="chip bg-color">基础</span>
                    </a>
                    
                </div>
                
            </div>
        </div>
        
    </div>
</article>

</div>


<script>
    $('#articleContent').on('copy', function (e) {
        // IE8 or earlier browser is 'undefined'
        if (typeof window.getSelection === 'undefined') return;

        var selection = window.getSelection();
        // if the selection is short let's not annoy our users.
        if (('' + selection).length < Number.parseInt('120')) {
            return;
        }

        // create a div outside of the visible area and fill it with the selected text.
        var bodyElement = document.getElementsByTagName('body')[0];
        var newdiv = document.createElement('div');
        newdiv.style.position = 'absolute';
        newdiv.style.left = '-99999px';
        bodyElement.appendChild(newdiv);
        newdiv.appendChild(selection.getRangeAt(0).cloneContents());

        // we need a <pre> tag workaround.
        // otherwise the text inside "pre" loses all the line breaks!
        if (selection.getRangeAt(0).commonAncestorContainer.nodeName === 'PRE' || selection.getRangeAt(0).commonAncestorContainer.nodeName === 'CODE') {
            newdiv.innerHTML = "<pre>" + newdiv.innerHTML + "</pre>";
        }

        var url = document.location.href;
        newdiv.innerHTML += '<br />'
            + '来源: Angus<br />'
            + '文章作者: YangChao<br />'
            + '文章链接: <a href="' + url + '">' + url + '</a><br />'
            + '本文章著作权归作者所有，任何形式的转载都请注明出处。';

        selection.selectAllChildren(newdiv);
        window.setTimeout(function () {bodyElement.removeChild(newdiv);}, 200);
    });
</script>


<!-- 代码块功能依赖 -->
<script type="text/javascript" src="/libs/codeBlock/codeBlockFuction.js"></script>

<!-- 代码语言 -->

<script type="text/javascript" src="/libs/codeBlock/codeLang.js"></script>


<!-- 代码块复制 -->

<script type="text/javascript" src="/libs/codeBlock/codeCopy.js"></script>


<!-- 代码块收缩 -->

<script type="text/javascript" src="/libs/codeBlock/codeShrink.js"></script>


    </div>
    <div id="toc-aside" class="expanded col l3 hide-on-med-and-down">
        <div class="toc-widget card" style="background-color: white;">
            <div class="toc-title"><i class="far fa-list-alt"></i>&nbsp;&nbsp;目录</div>
            <div id="toc-content"></div>
        </div>
    </div>
</div>

<!-- TOC 悬浮按钮. -->

<div id="floating-toc-btn" class="hide-on-med-and-down">
    <a class="btn-floating btn-large bg-color">
        <i class="fas fa-list-ul"></i>
    </a>
</div>


<script src="/libs/tocbot/tocbot.min.js"></script>
<script>
    $(function () {
        tocbot.init({
            tocSelector: '#toc-content',
            contentSelector: '#articleContent',
            headingsOffset: -($(window).height() * 0.4 - 45),
            collapseDepth: Number('0'),
            headingSelector: 'h2, h3, h4'
        });

        // Set scroll toc fixed.
        let tocHeight = parseInt($(window).height() * 0.4 - 64);
        let $tocWidget = $('.toc-widget');
        $(window).scroll(function () {
            let scroll = $(window).scrollTop();
            /* add post toc fixed. */
            if (scroll > tocHeight) {
                $tocWidget.addClass('toc-fixed');
            } else {
                $tocWidget.removeClass('toc-fixed');
            }
        });

        
        /* 修复文章卡片 div 的宽度. */
        let fixPostCardWidth = function (srcId, targetId) {
            let srcDiv = $('#' + srcId);
            if (srcDiv.length === 0) {
                return;
            }

            let w = srcDiv.width();
            if (w >= 450) {
                w = w + 21;
            } else if (w >= 350 && w < 450) {
                w = w + 18;
            } else if (w >= 300 && w < 350) {
                w = w + 16;
            } else {
                w = w + 14;
            }
            $('#' + targetId).width(w);
        };

        // 切换TOC目录展开收缩的相关操作.
        const expandedClass = 'expanded';
        let $tocAside = $('#toc-aside');
        let $mainContent = $('#main-content');
        $('#floating-toc-btn .btn-floating').click(function () {
            if ($tocAside.hasClass(expandedClass)) {
                $tocAside.removeClass(expandedClass).hide();
                $mainContent.removeClass('l9');
            } else {
                $tocAside.addClass(expandedClass).show();
                $mainContent.addClass('l9');
            }
            fixPostCardWidth('artDetail', 'prenext-posts');
        });
        
    });
</script>

    

</main>




    <footer class="page-footer bg-color">
    

    <div class="container row center-align"
         style="margin-bottom: 0px !important;">
        <div class="col s12 m8 l8 copy-right">
            Copyright&nbsp;&copy;
            
                <span id="year">2019-2023</span>
            
            <a href="/about" target="_blank">YangChao</a>
<!--             |&nbsp;Powered by&nbsp;<a href="https://hexo.io/" target="_blank">Hexo</a>
            |&nbsp;Theme&nbsp;<a href="https://github.com/Monkey-chao/Monkey-chao.github.io.git" target="_blank">Matery</a> -->
            <br>
            
                &nbsp;<i class="fas fa-chart-area"></i>&nbsp;站点总字数:&nbsp;<span
                        class="white-color">460.9k</span>
            
            
            
                
            
            
                <span id="busuanzi_container_site_pv">
                &nbsp;|&nbsp;<i class="far fa-eye"></i>&nbsp;总访问量:&nbsp;
                    <span id="busuanzi_value_site_pv" class="white-color"></span>
            </span>
            
            
                <span id="busuanzi_container_site_uv">
                &nbsp;|&nbsp;<i class="fas fa-users"></i>&nbsp;总访问人数:&nbsp;
                    <span id="busuanzi_value_site_uv" class="white-color"></span>
            </span>
            
            <br>

            <!-- 运行天数提醒. -->
            
            <br>
            
        </div>
<!--         <div class="col s12 m4 l4 social-link social-statis">
    <a href="https://github.com/Monkey-chao/Monkey-chao.github.io.git" class="tooltipped" target="_blank" data-tooltip="访问我的GitHub" data-position="top" data-delay="50">
        <i class="fab fa-github"></i>
    </a>




<!-- 

 -->


    <a href="tencent://AddContact/?fromId=50&fromSubId=1&subcmd=all&uin=1543373971" class="tooltipped" target="_blank" data-tooltip="QQ联系我: 1543373971" data-position="top" data-delay="50">
        <i class="fab fa-qq"></i>
    </a>


<!-- 




    <a href="/atom.xml" class="tooltipped" target="_blank" data-tooltip="RSS 订阅" data-position="top" data-delay="50">
        <i class="fas fa-rss"></i>
    </a>

 --></div>
 -->     </div>
</footer>

<div class="progress-bar"></div>


    <!-- 搜索遮罩框 -->
<div id="searchModal" class="modal">
    <div class="modal-content">
        <div class="search-header">
            <span class="title"><i class="fas fa-search"></i>&nbsp;&nbsp;搜索</span>
            <input type="search" id="searchInput" name="s" placeholder="请输入搜索的关键字"
                   class="search-input">
        </div>
        <div id="searchResult"></div>
    </div>
</div>

<script type="text/javascript">
$(function () {
    var searchFunc = function (path, search_id, content_id) {
        'use strict';
        $.ajax({
            url: path,
            dataType: "xml",
            success: function (xmlResponse) {
                // get the contents from search data
                var datas = $("entry", xmlResponse).map(function () {
                    return {
                        title: $("title", this).text(),
                        content: $("content", this).text(),
                        url: $("url", this).text()
                    };
                }).get();
                var $input = document.getElementById(search_id);
                var $resultContent = document.getElementById(content_id);
                $input.addEventListener('input', function () {
                    var str = '<ul class=\"search-result-list\">';
                    var keywords = this.value.trim().toLowerCase().split(/[\s\-]+/);
                    $resultContent.innerHTML = "";
                    if (this.value.trim().length <= 0) {
                        return;
                    }
                    // perform local searching
                    datas.forEach(function (data) {
                        var isMatch = true;
                        var data_title = data.title.trim().toLowerCase();
                        var data_content = data.content.trim().replace(/<[^>]+>/g, "").toLowerCase();
                        var data_url = data.url;
                        data_url = data_url.indexOf('/') === 0 ? data.url : '/' + data_url;
                        var index_title = -1;
                        var index_content = -1;
                        var first_occur = -1;
                        // only match artiles with not empty titles and contents
                        if (data_title !== '' && data_content !== '') {
                            keywords.forEach(function (keyword, i) {
                                index_title = data_title.indexOf(keyword);
                                index_content = data_content.indexOf(keyword);
                                if (index_title < 0 && index_content < 0) {
                                    isMatch = false;
                                } else {
                                    if (index_content < 0) {
                                        index_content = 0;
                                    }
                                    if (i === 0) {
                                        first_occur = index_content;
                                    }
                                }
                            });
                        }
                        // show search results
                        if (isMatch) {
                            str += "<li><a href='" + data_url + "' class='search-result-title'>" + data_title + "</a>";
                            var content = data.content.trim().replace(/<[^>]+>/g, "");
                            if (first_occur >= 0) {
                                // cut out 100 characters
                                var start = first_occur - 20;
                                var end = first_occur + 80;
                                if (start < 0) {
                                    start = 0;
                                }
                                if (start === 0) {
                                    end = 100;
                                }
                                if (end > content.length) {
                                    end = content.length;
                                }
                                var match_content = content.substr(start, end);
                                // highlight all keywords
                                keywords.forEach(function (keyword) {
                                    var regS = new RegExp(keyword, "gi");
                                    match_content = match_content.replace(regS, "<em class=\"search-keyword\">" + keyword + "</em>");
                                });

                                str += "<p class=\"search-result\">" + match_content + "...</p>"
                            }
                            str += "</li>";
                        }
                    });
                    str += "</ul>";
                    $resultContent.innerHTML = str;
                });
            }
        });
    };

    searchFunc('/search.xml', 'searchInput', 'searchResult');
});
</script>

    <!-- 回到顶部按钮 -->
<div id="backTop" class="top-scroll">
    <a class="btn-floating btn-large waves-effect waves-light" href="#!">
        <i class="fas fa-arrow-up"></i>
    </a>
</div>


    <script src="/libs/materialize/materialize.min.js"></script>
    <script src="/libs/masonry/masonry.pkgd.min.js"></script>
    <script src="/libs/aos/aos.js"></script>
    <script src="/libs/scrollprogress/scrollProgress.min.js"></script>
    <script src="/libs/lightGallery/js/lightgallery-all.min.js"></script>
    <script src="/js/matery.js"></script>

    

    
        
        <script type="text/javascript">
            // 只在桌面版网页启用特效
            var windowWidth = $(window).width();
            if (windowWidth > 768) {
                document.write('<script type="text/javascript" src="/libs/others/sakura.js"><\/script>');
            }
        </script>
    

    <!-- 雪花特效 -->
    

    <!-- 鼠标星星特效 -->
    

    

    <!-- Baidu Analytics -->

    <!-- Baidu Push -->

<script>
    (function () {
        var bp = document.createElement('script');
        var curProtocol = window.location.protocol.split(':')[0];
        if (curProtocol === 'https') {
            bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';
        } else {
            bp.src = 'http://push.zhanzhang.baidu.com/push.js';
        }
        var s = document.getElementsByTagName("script")[0];
        s.parentNode.insertBefore(bp, s);
    })();
</script>

    
    <script src="/libs/others/clicklove.js" async="async"></script>
    
    
    <script async src="/libs/others/busuanzi.pure.mini.js"></script>
    

    

    

    <!--腾讯兔小巢-->
    
    

    

    

    
    <script src="/libs/instantpage/instantpage.js" type="module"></script>
    

</body>

</html>
